Created
February 20, 2015 08:53
-
-
Save jeantil/388934cf9e2e0f3f2159 to your computer and use it in GitHub Desktop.
Filtering "None"'(s) out of an Hlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Hlister extends App { | |
import shapeless._ | |
case class A(value: Int) | |
case class B(value: Int) | |
case class C(value: Int) | |
trait folderLP extends Poly2 { | |
implicit def default[T, L <: HList] = at[T, L]((t,acc) => acc) | |
} | |
object folder extends folderLP { | |
implicit def some[T, L <: HList] = at[Some[T], L]((t, acc) => t.get :: acc) | |
} | |
trait SomeExtractor extends Poly1 { | |
implicit def default[T] = at[T]((t) => HNil) | |
} | |
object SomeExtractor extends SomeExtractor { | |
implicit def some[T] = at[Some[T]]((t)=> t.get :: HNil) | |
} | |
val result = Some(C(15)) :: Some(B(55)) :: Some(A(195)) :: HNil | |
val result2= Option(C(15)) :: Option(B(55)) :: Option(A(195)) :: HNil | |
val filtered = result.foldRight[HList](HNil: HNil)(folder) | |
val filtered2 = test.flatMap(SomeExtractor) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment