-
-
Save ivanopagano/282ec7112e29e63e603491961bdb2a5c to your computer and use it in GitHub Desktop.
Flattening deeply nested tuples with Shapeless
This file contains hidden or 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
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 | |
} | |
} | |
object FlattenHigh extends FlattenLow { | |
implicit object hnilFlatten extends Flatten[HNil, HNil] { | |
def apply(i: HNil) = HNil | |
} | |
implicit def hlistFlatten[H, T <: HList, HO <: HList, TO <: HList, O <: HList](implicit | |
hev: Flatten[H, HO], | |
tev: Flatten[T, TO], | |
pre: PrependAux[HO, TO, O] | |
) = new Flatten[H :: T, O] { | |
def apply(i: H :: T) = pre(hev(i.head), tev(i.tail)) | |
} | |
implicit def tupleFlatten[P <: Product, L <: HList, O <: HList](implicit | |
lev: HListerAux[P, L], | |
fev: Flatten[L, O] | |
) = new Flatten[P, O] { | |
def apply(i: P) = fev(lev(i)) | |
} | |
} | |
import FlattenHigh._ | |
def flatten[I, O <: HList](i: I)(implicit fev: Flatten[I, O], tev: Tupler[O]) = fev(i).tupled | |
val (a, b, c, d, e) = flatten((('a', "b"), ((3, 'd), 5.0))) | |
// Prints the following: | |
// a: Char = a | |
// b: String = b | |
// c: Int = 3 | |
// d: Symbol = 'd | |
// e: Double = 5.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment