Last active
December 19, 2015 03:49
-
-
Save stepancheg/5893139 to your computer and use it in GitHub Desktop.
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 printer.*; | |
| /// true iff type is HList | |
| [Something when Type?(Something)] | |
| define HList?(#Something): Bool private overload; | |
| [Head, Tail when HList?(Tail)] | |
| record HCons[Head, Tail]( | |
| head: Head, | |
| tail: Tail, | |
| ); | |
| record HNil(); | |
| default HList?(type) = false; | |
| [Head, Tail] | |
| overload HList?(#HCons[Head, Tail]) = true; | |
| overload HList?(#HNil) = true; | |
| [HList when HList?(HList)] | |
| define unpackHList(hlist: HList) private overload; | |
| [Head, Tail] | |
| overload unpackHList(hlist: HCons[Head, Tail]) = hlist.head, ..unpackHList(hlist.tail); | |
| overload unpackHList(hlist: HNil) = ; | |
| hlistToTuple(hlist) = [..unpackHList(hlist)]; | |
| define packHList(..values) private overload; | |
| overload packHList() = HNil(); | |
| [Head, ..Tail] | |
| overload packHList(head: Head, ..tail: Tail) = | |
| HCons(head, packHList(..tail)); | |
| tupleToHList(tuple) = packHList(..unpackTuple(tuple)); | |
| main() { | |
| println(hlistToTuple(HNil())); | |
| println(hlistToTuple(HCons(1, HNil()))); | |
| println(hlistToTuple(HCons(1, HCons(true, HNil())))); | |
| println(hlistToTuple(HCons(1, HCons(true, HCons('a', HNil()))))); | |
| // prints: | |
| // [] | |
| // [1] | |
| // [1, true] | |
| // [1, true, a] | |
| println(tupleToHList([])); | |
| println(tupleToHList([1])); | |
| println(tupleToHList([1, true])); | |
| println(tupleToHList([1, true, 'a'])); | |
| // prints: | |
| // HNil() | |
| // HCons[Int32, HNil](1, HNil()) | |
| // HCons[Int32, HCons[Bool, HNil]](1, HCons[Bool, HNil](true, HNil())) | |
| // HCons[Int32, HCons[Bool, HCons[Char, HNil]]](...) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment