Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@milessabin
milessabin / gist:8743568
Created January 31, 2014 21:28
Is breakOut just an optimization?
scala> import scala.collection._
import scala.collection._
scala> val l1: List[Int] = Set(1, 2, 3, 4).map(_/2).toList
l1: List[Int] = List(0, 1, 2)
scala> val l1: List[Int] = Set(1, 2, 3, 4).map(_/2)(breakOut)
l1: List[Int] = List(0, 1, 1, 2)
@milessabin
milessabin / gist:8820772
Last active August 29, 2015 13:56
Using type classes indexed by the singleton types of function values to statically verify interesting properties. Obviously this relies on the instance declaration telling the truth but, given that, it is nevertheless checkable at use sites.
scala> trait Pure[T]
defined trait Pure
scala> val plusOne: Int => Int = _ + 1
plusOne: Int => Int = <function1>
scala> val frobPlusOne: Int => Int = { i => println("frob") ; i + 1 }
frobPlusOne: Int => Int = <function1>
scala> implicit val plusOneIsPure = new Pure[plusOne.type]{}
scala> class somefn(j: Int) extends Poly1 {
| implicit def caseOne[A] = at[List[(Int,A)]]( xs => xs.filter(_._1 == j).map(_._2) )
| }
defined class somefn
scala> object t1 extends somefn(4)
defined module t1
scala> object t2 extends somefn(5)
defined module t2
@milessabin
milessabin / gist:8882700
Created February 8, 2014 12:08
Quick demo of shapeless's Comapped /cc @lars_h @jdegoes
scala> :paste
// Entering paste mode (ctrl-D to finish)
import shapeless._ ; import ops.hlist.Comapped
sealed trait Parser[A]
case class DummyParser[A]() extends Parser[A]
case class AllOf[L <: HList, O <: HList](parsers: L)
(implicit val cm: Comapped.Aux[L, Parser, O]) extends Parser[O]
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class Address(street : String, city : String, postcode : String)
case class Person(name : String, age : Int, address : Address)
// Exiting paste mode, now interpreting.
defined class Address
defined class Person
@milessabin
milessabin / gist:9991675
Created April 5, 2014 13:01
shapeless's Typeable in action ...
scala> import syntax.typeable._
import syntax.typeable._
scala> val wat: Any = List(1, 2, 3, 4)
wat: Any = List(1, 2, 3, 4)
scala> wat.cast[List[Int]].map(_.sum)
res0: Option[Int] = Some(10)
scala> val wat2: Any = "foo"
@milessabin
milessabin / gist:11141310
Created April 21, 2014 12:23
Old wine, new bottles: safely migrating/permuting/extending field values between case classes using shapeless 2.0.0's LabelledGeneric.
// See http://stackoverflow.com/questions/23192760/using-a-variable-as-an-argument-in-copy-on-a-case-class
import shapeless._, record._, syntax.singleton._, ops.hlist.Remove
/**
* This will be in shapeless 2.1.0 ...
*
* Permute the elements of the supplied `HList` of type `L` into the same order as the elements of
* the `HList` of type `M`.
*/
@milessabin
milessabin / gist:11244675
Created April 24, 2014 07:17
Lens inference in shapeless 2.0.0
import shapeless._
import ops.record.{ Selector, Updater }
import record.{ FieldType, field }
trait PathLens[T, P] {
type Elem
def get(t : T) : Elem
def set(t : T)(e : Elem) : T
}
@milessabin
milessabin / gist:c51b6851548dae403abf
Created May 9, 2014 10:11
Type safe selectDynamic without macros
miles@frege:~$ scala
Welcome to Scala version 2.11.0 (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.language.dynamics
import scala.language.dynamics
scala> case class Assoc[K, V](value: V)
defined class Assoc
@milessabin
milessabin / gist:88a21b1f2c6ec37ab6a7
Last active August 29, 2015 14:02
Deriving path-dependent type equality from value identity (in turn proved by singleton type inhabitation). For context see: https://gist.github.com/copumpkin/cb26bb921bbb81183299
scala> trait Foo { type T; val t: T }
defined trait Foo
scala> val x = new Foo { type T = Int ; val t = 23 }
x: Foo{type T = Int} = $anon$1@5aeea69b
scala> val y = new Foo { type T = String ; val t = "foo" }
y: Foo{type T = String} = $anon$1@7d7902b8
scala> def compare(a: Foo, b: Foo): Option[b.T] = a match { case a2: b.type => Some(a2.t) ; case _ => None }