Cartesian product of two or more lists: Difference between revisions
Cartesian product of two or more lists (view source)
Revision as of 11:57, 18 September 2019
, 4 years ago→Functional JS: Updated applicative version
(→Functional JS: Updated applicative version) |
|||
Line 1,114:
<lang Javascript>(() => {
// CARTESIAN PRODUCT OF TWO LISTS
// cartesianProduct :: [a] -> [b] -> [(a, b)]
const cartesianProduct =
ap(xs.map(
// GENERIC FUNCTIONS
// e.g. [(*2),(/2), sqrt] <*> [1,2,3]
Line 1,134:
// ap (<*>) :: [(a -> b)] -> [a] -> [b]
const ap =
// The sequential application of each of a list
// of functions to each of
f => xs.map(f)
);
// Tuple (,) :: a -> b -> (a, b)
// TEST -------------------------------------------------------------------▼
const Tuple = a => b => [a, b];
return [
cartesianProduct([1, 2]
cartesianProduct([3, 4]
cartesianProduct([1, 2]
cartesianProduct([]
]
.map(JSON.stringify)
|