Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
11
Task/Dot-product/JavaScript/dot-product-1.js
Normal file
11
Task/Dot-product/JavaScript/dot-product-1.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function dot_product(ary1, ary2) {
|
||||
if (ary1.length != ary2.length)
|
||||
throw "can't find dot product: arrays have different lengths";
|
||||
var dotprod = 0;
|
||||
for (var i = 0; i < ary1.length; i++)
|
||||
dotprod += ary1[i] * ary2[i];
|
||||
return dotprod;
|
||||
}
|
||||
|
||||
print(dot_product([1,3,-5],[4,-2,-1])); // ==> 3
|
||||
print(dot_product([1,3,-5],[4,-2,-1,0])); // ==> exception
|
||||
10
Task/Dot-product/JavaScript/dot-product-2.js
Normal file
10
Task/Dot-product/JavaScript/dot-product-2.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function dotp(x,y) {
|
||||
function dotp_sum(a,b) { return a + b; }
|
||||
function dotp_times(a,i) { return x[i] * y[i]; }
|
||||
if (x.length != y.length)
|
||||
throw "can't find dot product: arrays have different lengths";
|
||||
return x.map(dotp_times).reduce(dotp_sum,0);
|
||||
}
|
||||
|
||||
dotp([1,3,-5],[4,-2,-1]); // ==> 3
|
||||
dotp([1,3,-5],[4,-2,-1,0]); // ==> exception
|
||||
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