Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@milessabin
milessabin / gist:4973961
Last active December 13, 2015 20:58
Lazy pattern matching in Scala made even lazier with scalaz's Need.
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_05).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scalaz._
import scalaz._
scala> val (foo, bar, baz) = (Need({println("foo") ; "foo"}), Need({ println("bar") ; "bar"}), Need({ println("baz") ; "baz"}))
foo: scalaz.Need[java.lang.String] = scalaz.Need$$anon$4@1d90d034
bar: scalaz.Need[java.lang.String] = scalaz.Need$$anon$4@e551516
@milessabin
milessabin / Fogus.scala
Last active December 14, 2015 17:39 — forked from puffnfresh/Fogus.scala
import shapeless._
// fogus wanted this:
// [a] -> [b] -> [a b a b ...]
// Follow up question: "Do you know a dependent language with my interleave?"
// Yes. Scala and shapeless even make it a single line!
trait AB[-L <: HList, A, B]
@milessabin
milessabin / gist:5222090
Created March 22, 2013 15:19
Scala 2.9.x bug
// With -Ydependent-method-types
// Compiles with 2.10.x
scala> trait Algebra[F[_]] { trait Expr[A] }
defined trait Algebra
scala> trait ClosedExpr { def apply[F[_], A](algebra: Algebra[F]): algebra.Expr[A] }
defined trait ClosedExpr
scala> new ClosedExpr { def apply[F[_], A](algebra: Algebra[F]): algebra.Expr[A] = new algebra.Expr[A] {} }
package shapeless.examples
import shapeless.test.illTyped
object PantsOnFire {
//illTyped { 1+1 } // Does not compile
illTyped { illTyped { 1+1 } } // Compiles
val liar = illTyped { liar } // ???
}
@milessabin
milessabin / gist:5402966
Created April 17, 2013 09:22
shapeless coproduct WIP
/*
* Copyright (c) 2013 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@milessabin
milessabin / gist:5611702
Created May 20, 2013 11:25
Since when was "<:" an alias for "extends" ... not that I'm complaining, mind ;-)
scala> trait Foo ; trait Bar <: Foo
defined trait Foo
defined trait Bar
scala> implicitly[Bar <:< Foo]
res2: <:<[Bar,Foo] = <function1>
@milessabin
milessabin / gist:5637928
Created May 23, 2013 17:43
Mapping get over an HList of Maps.
import shapeless._
trait Name
case class VarName(nme: String) extends Name
case class ParamName(nme: String) extends Name
def lookup(nme: String): Option[Name] :: Option[Name] :: HNil = {
object get extends Poly1 {
implicit def default[T <: Name] = at[Map[String, T]](_.get(nme))
}
@milessabin
milessabin / gist:5831007
Created June 21, 2013 13:03
Case class members with shapeless 2.0.0-SNAPSHOT.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless._
import shapeless._
@milessabin
milessabin / gist:6015989
Last active December 19, 2015 20:49
Selecting type class instances by singleton types of values.
scala> import shapeless._
import shapeless._
scala> import SingletonTypes._
import SingletonTypes._
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Show[T] {
@milessabin
milessabin / gist:6081113
Last active December 20, 2015 05:49
Slicing and dicing tuples in shapeless 2.0.0-SNAPSHOT.
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless._
import shapeless._
scala> import syntax.tuple._
import syntax.tuple._