Skip to content

Instantly share code, notes, and snippets.

@heyrutvik
Created March 12, 2020 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyrutvik/6ebf44e8946b779519d43f30be70c4fa to your computer and use it in GitHub Desktop.
Save heyrutvik/6ebf44e8946b779519d43f30be70c4fa to your computer and use it in GitHub Desktop.
Code sample from the talk: High Wizardry in the Land of Scala by Daniel Spiewak
package demo
//talk: https://vimeo.com/28793245
//Kind
class HOMap[K[_], V[_]](delegate: Map[K[Any], V[Any]]) {
def apply[A](key: K[A]): V[A] =
delegate(key.asInstanceOf[K[Any]]).asInstanceOf[V[A]]
}
object HOMap {
type Pair[K[_], V[_]] = (K[A], V[A]) forSome { type A }
/**
* [error] type mismatch;
* [error] found : Seq[kind.HOMap.Pair[K,V]]
* [error] (which expands to) Seq[(K[A], V[A]) forSome { type A }]
* [error] required: Seq[(K[Any], V[Any])]
* [error] new HOMap[K, V](Map(tuples: _*))
* [error] ^
**/
def apply[K[_], V[_]](tuples: Pair[K, V]*) =
new HOMap[K, V](Map(tuples: _*))
}
//TypeLevel
object HList {
sealed trait HList {
type Head
type Tail <: HList
type Append[L <: HList] <: HList
def head: Head
def tail: Tail
def ++[L <: HList](xs: L): Append[L]
}
final class HNil extends HList {
type Head = Nothing
type Tail = Nothing
type Append[L <: HList] = L
def head = throw new Exception("head of nil")
def tail = throw new Exception("tail of nil")
def ::[A](a: A) = HCons(a, this)
def ++[L <: HList](xs: L): L = xs
}
val HNil = new HNil
case class HCons[A, B <: HList](head: A, tail: B) extends HList {
type Head = A
type Tail = B
type Append[L <: HList] = HCons[Head, Tail#Append[L]]
def ::[C](c: C) = HCons(c, this)
/**
* [error] value :: is not a member of HCons.this.tail.Append[L]
* [error] def ++[L <: HList](xs: L) = (head :: (tail ++ xs))
* [error] ^
**/
def ++[L <: HList](xs: L) = (head :: (tail ++ xs))
}
type ::[A, B <: HList] = HCons[A, B]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment