Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save reactormonk/548e6d66bef7944f4337 to your computer and use it in GitHub Desktop.

Select an option

Save reactormonk/548e6d66bef7944f4337 to your computer and use it in GitHub Desktop.
name := "scala-playground"
version := "0.1-SNAPSHOT"
organization := "org.reactormonk"
scalaVersion := "2.11.1"
resolvers ++= Seq(
"ScalaNLP Maven2" at "http://repo.scalanlp.org/repo",
"Scala Tools Snapshots" at "http://scala-tools.org/repo-snapshots/",
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
)
libraryDependencies ++= Seq(
"com.chuusai" %% "shapeless" % "2.0.0",
"org.scalaz" %% "scalaz-core" % "7.1.0"
)
javaOptions += "-Xmx4g"

HLists

Introduction

The Question

What are HLists, why do they exist and how do they work?

Code

https://gist.github.com/reactormonk/548e6d66bef7944f4337

█████████████████████████████████████████ █████████████████████████████████████████ ████ ▄▄▄▄▄ █▀▀ ███▄█▀█▀▄▄█ ▀█ ▄▄▄▄▄ ████ ████ █ █ █▄▀██▀▀▄▄▀▄ █▀ ▄█ █ █ ████ ████ █▄▄▄█ █ ▄ █ ██▀▀▀▄▀ ▀ ▄▀█ █▄▄▄█ ████ ████▄▄▄▄▄▄▄█ █ ▀▄▀▄▀ ▀▄█ ▀ ▀ █▄▄▄▄▄▄▄████ ████▄▄▀ ▀█▄▄█▀█ ▀▄▀ ▀██▀▄█▄ █ ▄▄▀▄▄▀████ ████▀ ▄█ █▄██▀▀ ▄▄▀█▀▄▄ ▀▄▀ ▀▄█▀ ████ ████▄ ▄▄▀▄▄ ▀▄ █▀ █▀█ ██▀▄ █▀ ▀▀ ████ █████ █▄▀ ▄ ▄▀█ █▄▄▀▄▀█▀ ▄▄▀▀▄▄█ █ █████ ████▀▀ ▀ ▀▄ ▀█ ▀█ ▄ ▄▄▄▄ ▄█▄█▄▀▀█▄████ ████ ▄▀▀▀▀▄ █▀▀▄ █ ▀▀ ▀█▄▀█▀▀ ▄██ █▄▄████ ████▄ █▀▄▄▄ ▀█ ▀██▀███ ▄▄▄██ ▀▀█▄▀▄▀████ ████▄ █▄█ █▄███▀ ▄█ ▄ ▀▀ ▄██ █▀ ████ ████▄█▄▄█▄▄▄ ▄▄▀ ██▀▀ ▀█▄▀ ▄▄▄ ███▀████ ████ ▄▄▄▄▄ █▄█ █ ▀▀▀▄ █▀ ██ █▄█ ██ █████ ████ █ █ █▀█ ▄█▄▄▀▄▄ ▄▀▄▄▀ ▄ █▀█▄████ ████ █▄▄▄█ █▀▀█▀█ ██▄▀█▀▀ ▄▀ ▀▀▄▄▄▄█▄████ ████▄▄▄▄▄▄▄█▄█▄▄▄█▄█▄██▄█▄▄█▄█▄█▄██▄█████ █████████████████████████████████████████ █████████████████████████████████████████

What are HLists?

Let’s take a peek

import shapeless._
val list = 1 :: "foo" :: HNil
val s = list(1)

Tuples

import shapeless.syntax._
val list = 2 :: 1 :: "foo" :: HNil
val result = list.tupled

Why I use them

  • data store for different types

Working with HLists

Reversing

val list = 1 :: "foo" :: HNil
val result = list.reverse

Mapping

val 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)

Selecting Elements

val result = (1 :: 2 :: "foo" :: "bar" :: HNil).reverse.select[String]

Removing Elements

val result = (1 :: 2 :: "foo" :: "bar" :: HNil).removeElem[String]

Ok, let’s pass HLists around

… simple

def firstIntPlusOne(list: HList): Int = list.select[Int] + 1
val res = firstIntPlusOne("foo" :: 1 :: HNil)

Maybe not.

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)

Returning HLists

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] + 1

Returning HLists, the second

import 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))

How do the methods work?

Select

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)
      }
}

Credits

Miles Sabin

https://github.com/milessabin

Shapeless

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment