Kronecker product: Difference between revisions

→‎JS ES6: Changed top level of kprod to concatMap - a little cleaner
m (fix)
(→‎JS ES6: Changed top level of kprod to concatMap - a little cleaner)
Line 220:
const concat = xs => [].concat.apply([], xs);
 
// curryconcatMap :: ((a, b) -> c[b]) -> [a] -> [b -> c]
const curryconcatMap = (f, => axs) => b => f[].concat.apply(a[], bxs.map(f));
 
// 2 or more arguments
// curry :: Function -> Function
const curry = (f, ...args) => {
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
function () {
return go(xs.concat([].slice.apply(arguments)));
};
return go([].slice.call(args, 1));
};
 
// map :: (a -> b) -> [a] -> [b]
Line 241 ⟶ 251:
// kprod :: [[Num]] -> [[Num]] -> [[Num]]
const kprod = (xs, ys) =>
concatconcatMap(xs.map(map(f(ys)))
.map(m => map(concat, transpose(m)),
.map(concatmap(f(ys)), xs);
);
 
// (* n) mapped over each element in a matrix
// f :: [[Num]] -> Num -> [[Num]]
const f = curry((mx, n) => mx.map(map(x => x * n), mx));
 
// TEST -------------------------------------------------------------------
return unlines(map(rows => unlines(map(show, rows)), [
kprod([
[1, 2],
Line 267 ⟶ 278:
[1, 1, 1, 1]
])
]));
].map(rows => unlines(rows.map(show))));
})();</lang>
{{Out}}
9,659

edits