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].concat(x)]
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 =>
let v = a,
xs.reduceRight((a, x) => f(x)(a), a);
i = xs.length;
while (i--) v = f(xs[i])(v);
return v;
};


// intercalate :: String -> [a] -> String
// intercalate :: String -> [a] -> String