Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. // HList
  2. class HList { }
  3. class HNil extends HList {
  4. static readonly instance = new HNil();
  5. private constructor() {
  6. super();
  7. }
  8. }
  9.  
  10. class HCons<Value, Next extends HList> extends HList {
  11. constructor(readonly value: Value, readonly next: Next) {
  12. super()
  13. }
  14. }
  15.  
  16. function hcons<V, Next extends HList>(value: V, next: Next) { return new HCons<V, Next>(value, next); }
  17. const hnil = HNil.instance;
  18.  
  19. // sample
  20. var t = hcons(1, hcons("lol", hcons(true, hnil))); // t: HCons<number, HCons<string, HCons<boolean, HNil>>>
  21.  
  22.  
  23. // HMap
  24. interface Pair<Key, Val> {
  25. key: Key;
  26. value: Val
  27. }
  28. function hmap<K, V, N extends HList>(key: K, value: V, next: N): HCons<Pair<K,V>, N> { return hcons({key:key, value: value}, next); }
  29.  
  30. //sample
  31. var map = hmap<"a", number, HCons<Pair<"b", boolean>, HNil>>("a", 1, hmap<"b", boolean, HNil>("b", true, hnil));
  32. // map: HCons<Pair<"a", number>, HCons<Pair<"b", boolean>, HNil>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement