2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
21
Task/Dot-product/JavaScript/dot-product-3.js
Normal file
21
Task/Dot-product/JavaScript/dot-product-3.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// dotProduct :: [Int] -> [Int] -> Int
|
||||
const dotProduct = (xs, ys) => {
|
||||
const sum = xs => xs ? xs.reduce((a, b) => a + b, 0) : undefined;
|
||||
|
||||
return xs.length === ys.length ? (
|
||||
sum(zipWith((a, b) => a * b, xs, ys))
|
||||
) : undefined;
|
||||
}
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = (f, xs, ys) => {
|
||||
const ny = ys.length;
|
||||
return (xs.length <= ny ? xs : xs.slice(0, ny))
|
||||
.map((x, i) => f(x, ys[i]));
|
||||
}
|
||||
|
||||
return dotProduct([1, 3, -5], [4, -2, -1]);
|
||||
})();
|
||||
1
Task/Dot-product/JavaScript/dot-product-4.js
Normal file
1
Task/Dot-product/JavaScript/dot-product-4.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
3
|
||||
Loading…
Add table
Add a link
Reference in a new issue