Skip to content

Instantly share code, notes, and snippets.

Miles Sabin milessabin

Block or report user

Report or block milessabin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@milessabin
milessabin / gist:8743568
Created Jan 31, 2014
Is breakOut just an optimization?
View gist:8743568
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 Aug 29, 2015
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.
View gist:8820772
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]{}
View gist:8821317
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 Feb 8, 2014
Quick demo of shapeless's Comapped /cc @lars_h @jdegoes
View gist:8882700
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]
View gist:9969589
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 Apr 5, 2014
shapeless's Typeable in action ...
View gist:9991675
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 Apr 21, 2014
Old wine, new bottles: safely migrating/permuting/extending field values between case classes using shapeless 2.0.0's LabelledGeneric.
View gist:11141310
// 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 Apr 24, 2014
Lens inference in shapeless 2.0.0
View gist:11244675
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
Type safe selectDynamic without macros
View gist:c51b6851548dae403abf
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 Aug 29, 2015
Deriving path-dependent type equality from value identity (in turn proved by singleton type inhabitation). For context see: https://gist.github.com/copumpkin/cb26bb921bbb81183299
View gist:88a21b1f2c6ec37ab6a7
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 }
You can’t perform that action at this time.