Skip to content

Instantly share code, notes, and snippets.

View cvogt's full-sized avatar

Jan Christopher Vogt cvogt

  • Symbiont.io
View GitHub Profile
@cvogt
cvogt / gist:b86aff8ac51f49a574cc
Last active August 29, 2015 14:11
Immutable cycles (as seen in "Case classes a la carte with shapeless, now!" at scalax2014 by @milessabin)
// immutable cycle
class Node[T]( val value: T, _next: => Node[T] ){
lazy val next = _next
}
val cycle: Node[Int] = new Node( 1, new Node( 2, cycle ) )
// prints List(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
println(cycle.iterator.take(10).map(_.value).toList)
implicit class NodeIterator[T](node: Node[T]){
@cvogt
cvogt / gist:71f8e4c241997ed711fb
Last active August 29, 2015 14:11
Polymorphic lists in Haskell (without using a closed ADT)
{-# LANGUAGE ExistentialQuantification #-}
-- type "hierarchy"
class VehicleType a where
description :: a -> String
data Car = Car String Int -- model year
instance VehicleType Car where
description (Car model year)
= "Car " ++ model ++ " " ++ (show year)
@cvogt
cvogt / gist:ca25a09ccb0b91d775f8
Created February 1, 2015 06:58
String type hash codes not the same
Welcome to Scala version 2.11.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_55).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._
scala> typeOf[java.lang.String].dealias.##
res0: Int = -1457551251
scala> new{def name = "Int"}.name
res0: String = Int
scala> new{def name = "Int"}
res1: AnyRef{def name: String} = $anon$1@73f9d297
scala> res1.name
warning: there was one feature warning; re-run with -feature for details
res2: String = Int
@cvogt
cvogt / gist:627a1aabeadf1d017920
Last active August 29, 2015 14:16
Scalacheck arbitrary case class generator (nested types, but no ADT support). See https://gist.github.com/cvogt/529a979f1767aba7629b
scala> case class Person(name: String, age:Int)
defined class Person
scala> import org.scalacheck._
import org.scalacheck._
scala> Gen.resultOf((Person.apply _).tupled).sample
res0: Option[Person] = Some(Person(祹ᨳ츜呣썷﹢偺ꉕォ资曪꫰︯㶄헖룗燠ᮆ첫谝팳뺑赊⑏㈛뷝Ҽ贽暩鑸㮩㸢綃ﯝ媳瀨鑾斉严郋ꪝ莇鯯샄❯⇚䉙虒ꁝ䰙ᑶ웮絶ꄝ弖䧗ꌦst྾朘ࣳ㞳섡嘇᪴,-1789025436))
scala> GenMore.resultOf((LargeCaseClass.apply _).tupled).sample
@cvogt
cvogt / gist:529a979f1767aba7629b
Last active August 29, 2015 14:16
Replacement for testing large case classes with Scalacheck until https://github.com/rickynils/scalacheck/pull/151 is released. Usage: https://gist.github.com/cvogt/529a979f1767aba7629b
/**
Defines missing implicit [[org.scalacheck.Arbitrary]] instances for tuples and functions
Hopefully supported by scalacheck out of the box soon
https://github.com/rickynils/scalacheck/pull/151
*/
package org.scalacheck
object ArbitraryMore{
// Functions //
/** Arbitrary instance of Function6 */
@cvogt
cvogt / nope.scala
Last active August 29, 2015 14:17 — forked from paulp/nope.scala
import Predef.{any2stringadd => _,_}
// GUESS THE INFERRED TYPE FOR ys!
class :=[T,Q]
object := {
/** Ignore default */
implicit def useProvided[P, D] : P := D = new (P := D)
/** Infer type argument to default */
implicit def useDefault[D] : D := D = new (D := D)
}
@cvogt
cvogt / gist:17c3652780b5f65db4ad
Last active August 29, 2015 14:17
Better Default than Nothing
scala> :paste
class :=[T,Q]
object :={
/** Ignore Default */
implicit def useProvided[Provided,Default] = new :=[Provided,Default]
/** Infer type argument as Default */
implicit def useDefault[Default] = new :=[Default,Default]
}
scala> def bar[T](implicit ct: reflect.ClassTag[T]): T = { println(ct.toString); null.asInstanceOf[T] }
@cvogt
cvogt / gist:b224af4b70a0c7d9a4cd
Last active August 29, 2015 14:20
3 ways of encoding Ada-style derived types in Scala
object Take1{
case class DurationFactory[T](create: Int => T)
trait Duration{
def duration: Int
}
case class MeetingDuration(duration: Int) extends Duration
implicit val factoryMeetingDuration = DurationFactory(new MeetingDuration(_:Int))
case class ConstraintDuration(duration: Int) extends Duration
@cvogt
cvogt / gist:a46318bc1cfb75fc1f35
Last active August 29, 2015 14:22
No-magic Scala test framework concept
// admittedly there is some optional implicit magic below to reduce some boiler place,
// but it is only using implicits, not runtime reflection as many test frameworks do
// usage: stand-alone test
object MyTest extends Test(
assert(2 == 1+1)
)
// usage: test suite
object MySuite extends TestSuite(