Created
March 6, 2019 20:25
-
-
Save ekozhura/f8e57282c530cf2dca7ac993b139608f 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
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