What are HLists, why do they exist and how do they work?
https://gist.github.com/reactormonk/548e6d66bef7944f4337
█████████████████████████████████████████ █████████████████████████████████████████ ████ ▄▄▄▄▄ █▀▀ ███▄█▀█▀▄▄█ ▀█ ▄▄▄▄▄ ████ ████ █ █ █▄▀██▀▀▄▄▀▄ █▀ ▄█ █ █ ████ ████ █▄▄▄█ █ ▄ █ ██▀▀▀▄▀ ▀ ▄▀█ █▄▄▄█ ████ ████▄▄▄▄▄▄▄█ █ ▀▄▀▄▀ ▀▄█ ▀ ▀ █▄▄▄▄▄▄▄████ ████▄▄▀ ▀█▄▄█▀█ ▀▄▀ ▀██▀▄█▄ █ ▄▄▀▄▄▀████ ████▀ ▄█ █▄██▀▀ ▄▄▀█▀▄▄ ▀▄▀ ▀▄█▀ ████ ████▄ ▄▄▀▄▄ ▀▄ █▀ █▀█ ██▀▄ █▀ ▀▀ ████ █████ █▄▀ ▄ ▄▀█ █▄▄▀▄▀█▀ ▄▄▀▀▄▄█ █ █████ ████▀▀ ▀ ▀▄ ▀█ ▀█ ▄ ▄▄▄▄ ▄█▄█▄▀▀█▄████ ████ ▄▀▀▀▀▄ █▀▀▄ █ ▀▀ ▀█▄▀█▀▀ ▄██ █▄▄████ ████▄ █▀▄▄▄ ▀█ ▀██▀███ ▄▄▄██ ▀▀█▄▀▄▀████ ████▄ █▄█ █▄███▀ ▄█ ▄ ▀▀ ▄██ █▀ ████ ████▄█▄▄█▄▄▄ ▄▄▀ ██▀▀ ▀█▄▀ ▄▄▄ ███▀████ ████ ▄▄▄▄▄ █▄█ █ ▀▀▀▄ █▀ ██ █▄█ ██ █████ ████ █ █ █▀█ ▄█▄▄▀▄▄ ▄▀▄▄▀ ▄ █▀█▄████ ████ █▄▄▄█ █▀▀█▀█ ██▄▀█▀▀ ▄▀ ▀▀▄▄▄▄█▄████ ████▄▄▄▄▄▄▄█▄█▄▄▄█▄█▄██▄█▄▄█▄█▄█▄██▄█████ █████████████████████████████████████████ █████████████████████████████████████████
import shapeless._
val list = 1 :: "foo" :: HNil
val s = list(1)import shapeless.syntax._
val list = 2 :: 1 :: "foo" :: HNil
val result = list.tupled- data store for different types
val list = 1 :: "foo" :: HNil
val result = list.reverseval list = 1 :: "foo" :: HNil
val plusOne = new Poly1 {
implicit def caseInt = at[Int](_ + 1)
implicit def caseString = at[String](_ + 1)
}
import plusOne._
val result = list.map(plusOne)val result = (1 :: 2 :: "foo" :: "bar" :: HNil).reverse.select[String]val result = (1 :: 2 :: "foo" :: "bar" :: HNil).removeElem[String]def firstIntPlusOne(list: HList): Int = list.select[Int] + 1
val res = firstIntPlusOne("foo" :: 1 :: HNil)import ops.hlist._
def firstIntPlusTwo[L <: HList](list: L)(implicit sel: Selector[L, Int]): Int = list.select[Int] + 2
def firstIntPlusOne[L <: HList](list: L)(implicit sel: Selector[L, Int]): Int = firstIntPlusTwo(list) + 1
val res = firstIntPlusOne("foo" :: 1 :: HNil)import ops.hlist._
def removeString[L <: HList, R <: HList](list: L)(implicit rem: Remove[L, String]): R = list.removeElem[String]
def firstIntPlusOne[L <: HList](list: L)(implicit sel: Selector[L, Int]): Int = list.select[Int] + 1import ops.hlist._
def reverseAndInt[L <: HList, R <: HList](list: L)(implicit rev: Reverse.Aux[L, R]): Int :: R = 1 :: list.reverse
def firstIntPlusOne[L <: HList](list: L)(implicit sel: Selector[L, Int]): Int = list.select[Int] + 1
val res = firstIntPlusOne(reverseAndInt(2 :: "foo" :: HNil))trait DepFn1[T] {
type Out
def apply(t: T): Out
}
trait Selector[L <: HList, U] extends DepFn1[L] { type Out = U }
object Selector {
def apply[L <: HList, U](implicit selector: Selector[L, U]): Aux[L, U] = selector
type Aux[L <: HList, U] = Selector[L, U]
implicit def select[H, T <: HList]: Aux[H :: T, H] =
new Selector[H :: T, H] {
def apply(l : H :: T) = l.head
}
implicit def recurse[H, T <: HList, U]
(implicit st : Selector[T, U]): Aux[H :: T, U] =
new Selector[H :: T, U] {
def apply(l : H :: T) = st(l.tail)
}
}Alexandre Archambault <alexandre.archambault@gmail.com> @alxarchambault Alois Cochard <alois.cochard@gmail.com> @aloiscochard Ben Hutchison <brhutchison@gmail.com> @ben_hutchison Ben James <ben.james@guardian.co.uk> @bmjames Brian McKenna <brian@brianmckenna.org> @puffnfresh Chris Hodapp <clhodapp1@gmail.com> @clhodapp Cody Allen <ceedubs@gmail.com> @fourierstrick Dario Rexin <dario.rexin@r3-tech.de> @evonox George Leontiev <folone@gmail.com> @folone Huw Giddens <hgiddens@gmail.com> Jason Zaugg <jzaugg@gmail.com> @retronym Johannes Rudolph <johannes.rudolph@gmail.com> @virtualvoid Joni Freeman <joni.freeman@ri.fi> @jonifreeman Julien Tournay <boudhevil@gmail.com> @skaalf Kevin Wright <kev.lee.wright@gmail.com> @thecoda Lars Hupel <lars.hupel@mytum.de> @larsr_h Mathias Doenitz <mathias@spray.io> @sirthias Michael Donaghy <md401@srcf.ucam.org> Michael Pilquist <mpilquist@gmail.com> @mpilquist Miles Sabin <miles@milessabin.com> @milessabin Nikolas Evangelopoulos <nikolas@jkl.gr> Paolo G. Giarrusso <p.giarrusso@gmail.com> @blaisorblade Stacy Curl <stacy.curl@gmail.com> @stacycurl Stephen Compall <scompall@nocandysw.com> @S11001001 Tom Switzer <thomas.switzer@gmail.com> @tixxit Travis Brown <travisrobertbrown@gmail.com> @travisbrown