Dot product: Difference between revisions
Content deleted Content added
m →{{header|Haskell}}: Tidied the Maybe version a little |
m →JS ES6: Tidied |
||
Line 1,593:
<syntaxhighlight lang="javascript">(() => {
// ------------------- DOT PRODUCT -------------------
// dotProduct :: [Int] -> [Int] -> Int
const dotProduct =
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
// --------------------- GENERIC ---------------------
// mul :: Num -> Num -> Num
const mul = x =>
y => x * y;
// sum :: [Num] -> Num
const sum = xs =>
// The numeric sum of all values in xs.
return xs.length === ys.length ? (▼
▲ sum(zipWith((a, b) => a * b, xs, ys))
▲ ) : undefined;
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith =
// A list constructed by zipping with a
// custom function, rather than with the
// default tuple constructor.
▲ .map((x, i) => f(x, ys[i]));
xs => ys => xs.map(
(x, i) => f(x)(ys[i])
).slice(
);
// MAIN ---
▲ return dotProduct([1, 3, -5], [4, -2, -1]);
return main();
})();</syntaxhighlight>
=={{header|jq}}==
|