Skip to content

Instantly share code, notes, and snippets.

@milessabin
milessabin / gist:6256495
Last active March 10, 2018 02:26
Path dependent types with an implicit prefix. We can infer the singleton type prefix of a path dependent type and then implicitly summon the unique value of that singleton type.
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> class Prefix { class Dep { type P = Prefix.this.type } }
defined class Prefix
scala> val (p1, p2) = (new Prefix, new Prefix)
p1: Prefix = Prefix@2212c414
p2: Prefix = Prefix@7e070e85
@milessabin
milessabin / gist:6185537
Created August 8, 2013 15:18
shapeless records ... current state of play ...
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 SingletonTypes._ ; import Record._
import shapeless._
import SingletonTypes._
import Record._
scala> :paste
@edwinb
edwinb / reflect.idr
Last active December 20, 2015 15:08
Automated list associativity proofs, by reflection, in Idris.
module Reflect
import Decidable.Equality
using (xs : List a, ys : List a, G : List (List a))
data Elem : a -> List a -> Type where
Stop : Elem x (x :: xs)
Pop : Elem x ys -> Elem x (y :: xs)
22:06 ~/Projects/Kepler_5923/sandbox (ticket/5923)$ cat Macros.scala
import language.experimental.macros
import scala.reflect.macros.Context
trait Iso[T, U] {
def to(t : T) : U
// def from(u : U) : T
}
object Iso {
@aztek
aztek / FRP.scala
Last active December 16, 2015 03:59
Trivial FRP for Scala with scala-idioms
import idioms._ // http://github.com/aztek/scala-idioms
trait Cell[T] {
def ! : T
def := (value: T) { throw new UnsupportedOperationException }
}
val frp = new Idiom[Cell] {
def pure[A](a: ⇒ A) = new Cell[A] {
private var value = a
@paulp
paulp / transcript
Last active December 15, 2015 13:09
scala> class Bippy(xs: List[Int]) extends improving.TypesafeProxy(xs) { def isEmpty = true }
defined class Bippy
scala> val bippy = new Bippy(1 to 10 toList)
bippy: Bippy = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> bippy.slice(3, 7)
[proxy] $line4.$read.$iw.$iw.bippy.slice(3, 7)
res1: List[Int] = List(4, 5, 6, 7)
@travisbrown
travisbrown / nat-example.scala
Created January 7, 2013 12:26
Creating a Nat type from a compile-time literal
import scala.language.experimental.macros
import scala.reflect.macros.Context
import shapeless._
object NatExample {
def toNat(n: Int): Any = macro toNat_impl
def toNat_impl(c: Context)(n: c.Expr[Int]) = {
import c.universe._
@travisbrown
travisbrown / view-application.scala
Created November 22, 2012 14:39
Weird view application observation
import scalaz._
import scalaz.std.option._
import scalaz.syntax.functor._
trait Inc extends (Int => Int) {
implicit def incToLiftable(i: this.type): {
def lift(implicit F: Functor[Option]): Option[Int] => Option[Int]
} = implicitly[this.type => LiftV[Option, Int, Int]].apply(i)
def apply(x: Int) = x + 1
}
@travisbrown
travisbrown / needle-in-haystack.scala
Created November 13, 2012 22:57
Digging through arbitrarily nested case classes, tuples, and lists
/**
* Digging through arbitrarily nested case classes, tuples, and lists
* by Travis Brown
*
* In response to this question by Channing Walton on the Shapeless dev list:
*
* https://groups.google.com/d/msg/shapeless-dev/hn7_U21tupI/Zm9h3uNb51gJ
*
* Tested with Scala 2.9.2 and Shapeless 1.2.3. Should work on 1.2.2 with minor edits.
*/
@travisbrown
travisbrown / tuple-flatten.scala
Created October 24, 2012 11:18
Flattening deeply nested tuples with Shapeless
import shapeless._
trait Flatten[I, O <: HList] {
def apply(i: I): O
}
trait FlattenLow {
implicit def otherFlatten[I] = new Flatten[I, I :: HNil] {
def apply(i: I) = i :: HNil
}