Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HList
- class HList { }
- class HNil extends HList {
- static readonly instance = new HNil();
- private constructor() {
- super();
- }
- }
- class HCons<Value, Next extends HList> extends HList {
- constructor(readonly value: Value, readonly next: Next) {
- super()
- }
- }
- function hcons<V, Next extends HList>(value: V, next: Next) { return new HCons<V, Next>(value, next); }
- const hnil = HNil.instance;
- // sample
- var t = hcons(1, hcons("lol", hcons(true, hnil))); // t: HCons<number, HCons<string, HCons<boolean, HNil>>>
- // HMap
- interface Pair<Key, Val> {
- key: Key;
- value: Val
- }
- 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); }
- //sample
- var map = hmap<"a", number, HCons<Pair<"b", boolean>, HNil>>("a", 1, hmap<"b", boolean, HNil>("b", true, hnil));
- // map: HCons<Pair<"a", number>, HCons<Pair<"b", boolean>, HNil>>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement