Matrix multiplication: Difference between revisions
→JS ES6: Adjusted primitives, tidied.
(→JS ES6: Adjusted primitives, tidied.) |
|||
Line 3,756:
===ES6===
<lang JavaScript>((() => {
// matrixMultiply :: Num a => [[a]] -> [[a]] -> [[a]]
Line 3,762:
b => {
const cols = transpose(b);
return a.map(
compose(
dotProduct
)
};
Line 3,774 ⟶ 3,775:
compose(sum, zipWith(mul)(xs));
//
const main = () =>
JSON.stringify(matrixMultiply(
Line 3,788 ⟶ 3,790:
]));
//
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
Line 3,797 ⟶ 3,802:
);
x => y => f(y)(x);▼
xs.length▼
// mul :: Num a => a -> a -> a
const mul = a =>
b => a * b;
// sum :: (Num a) => [a] -> a
Line 3,826 ⟶ 3,812:
xs.reduce((a, x) => a + x, 0);
// transpose :: [[a]] -> [[a]]
Line 3,837 ⟶ 3,817:
// The columns of the input transposed
// into new rows.
//
(_,
)
) : [];
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Line 3,849 ⟶ 3,831:
// custom function, rather than with the
// default tuple constructor.
xs => ys =>
0, Math.min(xs.length,
// MAIN ---
|