Created
October 9, 2012 02:55
-
-
Save etorreborre/3856308 to your computer and use it in GitHub Desktop.
Tupling big HLists
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
import shapeless._ | |
// if there's only one element, return it | |
trait ToTuple2 extends Poly1 { | |
implicit def h2[T] = at[T :: HNil] { case t :: HNil => t } | |
} | |
// if there are at least 2 elements, tuple them | |
trait ToTuple1 extends ToTuple2 { | |
implicit def h1[T1, T2, L <: HList, T <: T1 :: T2 :: L](implicit tupler: Tupler[T1 :: T2 :: L]) = at[T1 :: T2 :: L] { | |
case l @ (t1 :: t2 :: rest) => l.tupled | |
} | |
} | |
// if there are more than 21 elements, tuple the first 21s and recursively tuple the rest | |
trait ToTuple0 extends ToTuple1 { | |
implicit def h0[T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19,T20,T21,R <: HList](implicit c: Case1[R]) = | |
at[T1::T2::T3::T4::T5::T6::T7::T8::T9::T10::T11::T12::T13::T14::T15::T16::T17::T18::T19::T20::T21::R] { | |
case t1::t2::t3::t4::t5::t6::t7::t8::t9::t10::t11::t12::t13::t14::t15::t16::t17::t18::t19::t20::t21::rest => | |
(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,c(rest)) | |
} | |
} | |
object toTuple extends ToTuple0 | |
toTuple(1::2::3::4::5::HNil) | |
// (1,2,3,4,5) | |
toTuple(1::2::3::4::5::6::7::8::9::10::11::12::13::14::15::16::17::18::19::20::21::22::HNil) | |
// (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22) | |
toTuple(1::2::3::4::5::6::7::8::9::10::11::12::13::14::15::16::17::18::19::20::21::22::23::24::HNil) | |
//(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,(22,23,24)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment