Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
46
Task/Dot-product/JavaScript/dot-product-3.js
Normal file
46
Task/Dot-product/JavaScript/dot-product-3.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// ------------------- DOT PRODUCT -------------------
|
||||
|
||||
// dotProduct :: [Num] -> [Num] -> Either Null Num
|
||||
const dotProduct = xs =>
|
||||
ys => xs.length === ys.length
|
||||
? sum(zipWith(mul)(xs)(ys))
|
||||
: null;
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
dotProduct([1, 3, -5])([4, -2, -1]);
|
||||
|
||||
|
||||
// --------------------- 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.
|
||||
xs.reduce((a, x) => a + x, 0);
|
||||
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f =>
|
||||
// A list constructed by zipping with a
|
||||
// custom function, rather than with the
|
||||
// default tuple constructor.
|
||||
xs => ys => xs.map(
|
||||
(x, i) => f(x)(ys[i])
|
||||
).slice(
|
||||
0, Math.min(xs.length, ys.length)
|
||||
);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue