Skip to content

Instantly share code, notes, and snippets.

View tpolecat's full-sized avatar
🔥
Dijkstra would not have liked this.

Rob Norris tpolecat

🔥
Dijkstra would not have liked this.
View GitHub Profile
@tpolecat
tpolecat / gist:6270850
Created August 19, 2013 16:09
view bound example
case class Box[A](a:A) {
def len1(implicit ev: A <:< Seq[_]) = a.length
def len2(implicit ev: A => Seq[_]) = a.length
}
Box(List(1,2,3)).len1 // ok
Box(List(1,2,3)).len2 // ok
Box("foo").len1 // doesn't compile
package util
import shapeless._
import shapeless.TypeOperators._
import shapeless.Nat._
import shapeless.LTEq._
import scala.annotation.implicitNotFound
@implicitNotFound("Cannot prove that ${A} is evenly divisble by ${B}")
trait Div[A <: Nat, B <: Nat]
object Alg {
sealed trait Num { def toInt: Int }
sealed trait Big extends Num
sealed trait Small extends Num
object Big {
def fromInt(n:Int): Option[Big] =
if (n >= 5) Some(new Big { def toInt = n }) else None
@tpolecat
tpolecat / gist:6208565
Last active December 20, 2015 22:58
Playing around with Nat literals and I'm finding cases (as below) where everything goes kind of sideways. Any clues?
// Scala 2.10.2 and Shapeless 2.0.0-SNAPSHOT
import shapeless._
import shapeless.nat._
sealed trait Month {
type ND <: Nat
def apply(n: Nat)(implicit ev: LTEq[n.N, ND]) = "foo"
}
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_13).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless._
import shapeless._
package time.calendar
import scalaz.Enum
import scalaz.Ordering
import shapeless._
import shapeless.Nat._
import shapeless._
import shapeless.Nat._
import shapeless.LTEq._
@tpolecat
tpolecat / gist:6128771
Last active December 20, 2015 12:09
TPayne's problem from http://dpaste.com/1325681/
object TPayne extends App {
val m = Map(
"2" -> List(List("1")),
"3" -> List(List("9"), List("5", "6", "7", "8")),
"1" -> List(List("8", "9"), List("2"), List("2", "3")))
def combos[A, B](xs: Map[A, List[B]]): List[Map[A, B]] =
scala> trait X { type A }
defined trait X
scala> implicit def convert(a:X)(implicit ev: a.A =:= String): Int = 42
convert: (a: X)(implicit ev: =:=[a.A,String])Int
scala> (new X {}) : Int
<console>:10: error: Cannot prove that a.A =:= String.
(new X {}) : Int
^
scala> trait X { type A }
defined trait X
scala> implicit def convert(a:X)(implicit ev: a.A =:= String): Int = 42
convert: (a: X)(implicit ev: =:=[a.A,String])Int
scala> (new X {}) : Int
<console>:10: error: Cannot prove that a.A =:= String.
(new X {}) : Int
^
package angles
import spire.algebra._
import scala.math.Pi
object Angle {
def apply(n: Double): Angle =
if (n < 0.0) new Angle(n % 360.0 + 360.0) else new Angle(n % 360.0)
def fromRadians(n: Double): Angle =
Angle(n * 360.0 / (2.0 * Pi))