Put flat_map in ListInternal.aes

This commit is contained in:
Ulf Norell 2019-08-27 11:33:43 +02:00
parent 02af75aa34
commit e7d3a5b9f2
2 changed files with 7 additions and 9 deletions

View File

@ -102,10 +102,8 @@ namespace List =
[] => reverse(acc) [] => reverse(acc)
h::t => map_(f, t, f(h)::acc) h::t => map_(f, t, f(h)::acc)
function flat_map(f : 'a => list('b), l : list('a)) : list('b) = flat_map_(f, l, []) function flat_map(f : 'a => list('b), l : list('a)) : list('b) =
private function flat_map_(f : 'a => list('b), l : list('a), acc : list('b)) : list('b) = switch(l) ListInternal.flat_map(f, l)
[] => reverse(acc)
h::t => flat_map_(f, t, reverse(f(h)) ++ acc)
function filter(p : 'a => bool, l : list('a)) : list('a) = filter_(p, l, []) function filter(p : 'a => bool, l : list('a)) : list('a) = filter_(p, l, [])
private function filter_(p : 'a => bool, l : list('a), acc : list('a)) : list('a) = switch(l) private function filter_(p : 'a => bool, l : list('a), acc : list('a)) : list('a) = switch(l)

View File

@ -1,9 +1,9 @@
namespace ListInternal = namespace ListInternal =
private function reverse_(xs : list('a), ys : list('a)) : list('a) = // -- Flatmap ----------------------------------------------------------------
function flat_map(f : 'a => list('b), xs : list('a)) : list('b) =
switch(xs) switch(xs)
[] => ys [] => []
x :: xs => reverse_(xs, x :: ys) x :: xs => f(x) ++ flat_map(f, xs)
function reverse(xs : list('a)) = reverse_(xs, [])