Skip to content

Instantly share code, notes, and snippets.

@ekozhura
Created March 6, 2019 20:25
Show Gist options
  • Save ekozhura/f8e57282c530cf2dca7ac993b139608f to your computer and use it in GitHub Desktop.
Save ekozhura/f8e57282c530cf2dca7ac993b139608f to your computer and use it in GitHub Desktop.
type lazylist('a) =
| Cons('a, unit => lazylist('a));
let rec lseq = n => Cons(n, () => lseq(n + 1));
let lhd = (Cons(n, _)) => n;
let ltl = (Cons(_, tf)) => tf();
let rec ltake = (Cons(h, tf), n) =>
switch (n) {
| 0 => []
| _ => [h, ...ltake(tf(), n - 1)]
};
let rec ldrop = (Cons(h, tf) as ll, n) =>
switch (n) {
| 0 => ll
| _ => ldrop(tf(), n - 1)
};
let rec lmap = (f, Cons(h, tf)) =>
Cons(f(h), () => lmap(f, tf()));
let rec lfilter = (f, Cons(h, tf)) =>
if (f(h)) {
Cons(h, () => lfilter(f, tf()));
} else {
lfilter(f, tf());
};
let squares = lmap(a => a * a, lseq(2));
Js.log(ltake(lfilter(x => x > 50, squares), 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment