Cartesian product of two or more lists: Difference between revisions
Content added Content deleted
(→Functional JS: Updated n-ary version.) |
(→Functional JS: n-Ary version - a simpler and more maintainable foldr (using reduceRight)) |
||
Line 1,170: | Line 1,170: | ||
bind(as)( |
bind(as)( |
||
x => bind(xs)( |
x => bind(xs)( |
||
a => [ |
a => [ |
||
[a].concat(x) |
|||
⚫ | |||
) |
) |
||
) |
) |
||
)([ |
)([ |
||
⚫ | |||
⚫ | |||
// TEST ------------------------------------------- |
// TEST ------------------------------------------- |
||
Line 1,204: | Line 1,208: | ||
// foldr :: (a -> b -> b) -> b -> [a] -> b |
// foldr :: (a -> b -> b) -> b -> [a] -> b |
||
const foldr = f => a => xs => |
const foldr = f => a => xs => |
||
xs.reduceRight((a, x) => f(x)(a), a); |
|||
⚫ | |||
while (i--) v = f(xs[i])(v); |
|||
⚫ | |||
⚫ | |||
// intercalate :: String -> [a] -> String |
// intercalate :: String -> [a] -> String |