Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@milessabin
milessabin / gist:88237c53916f8357b894
Created June 2, 2014 10:41
Dependent pattern matching in Scala or just a GADT?
trait Dep[T]
trait Pack[T] {
val dep: Dep[T]
val t: T
}
object Pack {
def apply[T](d: Dep[T], t0: T) =
new Pack[T] { val dep = d ; val t = t0 }
@milessabin
milessabin / gist:6da04d3ef340171ca2ca
Created June 9, 2014 21:26
Type safe record selection syntax for shapeless records.
scala> import shapeless._, record._, syntax.singleton._
import shapeless._
import record._
import syntax.singleton._
scala> val mary = ('name ->> "Mary" :: 'age ->> 23 :: HNil).record
mary: shapeless.syntax.DynamicRecordOps[shapeless.::[String with shapeless.record.KeyTag[Symbol with shapeless.tag.Tagged[String("name")],String],shapeless.::[Int with shapeless.record.KeyTag[Symbol with shapeless.tag.Tagged[String("age")],Int],shapeless.HNil]]] = DynamicRecordOps(Mary :: 23 :: HNil)
scala> mary.name
res2: String = Mary
@milessabin
milessabin / gist:2792ad2810ee9700399f
Created July 29, 2014 21:42
Any =:= WildcardType =:= Nothing
miles@frege:~$ scala
Welcome to Scala version 2.11.1 (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> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'. **
** scala.tools.nsc._ has been imported **
** global._, definitions._ also imported **
scala> :paste
// Entering paste mode (ctrl-D to finish)
sealed trait Content
case class A() extends Content
case class B() extends Content
// Exiting paste mode, now interpreting.
@milessabin
milessabin / gist:eeb3fd6fc669e1e42fc9
Last active August 29, 2015 14:10
Simple EitherT demo
package demo
import scalaz._
import scalaz.syntax._
import scalaz.effect.IO
import scalaz.effect.IO._
import scalaz.syntax.std.either._
object Demo {
type M[A] = EitherT[IO, Throwable, A]
object FlattenTuple {
import shapeless._
import ops.tuple.FlatMapper
import syntax.std.tuple._
trait LowPriorityFlattenTuple extends Poly1 {
implicit def default[T] = at[T](Tuple1(_))
}
scala> import ops.nat._
import ops.nat._
scala> def foo[T, N <: Nat](l: Sized[Seq[T], N])(implicit ev: LTEq[N, _3]) = l
foo: [T, N <: shapeless.Nat](l: shapeless.Sized[Seq[T],N])(implicit ev: shapeless.ops.nat.LTEq[N,shapeless.nat._3])shapeless.Sized[Seq[T],N]
scala> val l1 = Sized(1, 2, 3)
l1: shapeless.Sized[scala.collection.immutable.IndexedSeq[Int],shapeless.nat._3] = shapeless.Sized@17191095
scala> val l2 = Sized(1, 2, 3, 4)
@milessabin
milessabin / Foldable.scala
Last active August 29, 2015 14:17 — forked from xuwei-k/Foldable.scala
Trampolined to avoid stack overflow ...
import scala.util.control.TailCalls._
import shapeless._
trait Foldable[F[_]] {
def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B
}
object Foldable {
implicit def apply[F[_]](implicit fr: Lazy[FoldableRec[F]]): Foldable[F] =
package shapeless.examples
import shapeless._
import nat._
import ops.hlist._
import test._
object TableExample extends App {
final case class Row[L <: HList](cells: L)
@milessabin
milessabin / gist:18a9effd4bb354bd1dd4
Last active August 29, 2015 14:18
Demo of SingletonProductArgs from shapeless-2.2.0
scala> :paste
// Entering paste mode (ctrl-D to finish)
import shapeless._, ops.hlist._, ops.record._
object selectAll extends SingletonProductArgs {
class Apply[K <: HList] {
def from[T, R <: HList, S <: HList, Out](t: T)
(implicit
gen: LabelledGeneric.Aux[T, R],