Put flat_map in ListInternal.aes
This commit is contained in:
parent
02af75aa34
commit
e7d3a5b9f2
@ -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)
|
||||||
|
@ -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, [])
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user