Dot product: Difference between revisions

Content added Content deleted
m (→‎JS ES6: Tidied)
Line 1,590: Line 1,590:


===ES6===
===ES6===
Composing functional primitives into a '''dotProduct()''' which returns '''undefined''' (rather than an error) when the array lengths are unmatched.
Composing functional primitives into a '''dotProduct()''' which returns a '''null''' value (rather than an error) when the array lengths are unmatched.


<syntaxhighlight lang="javascript">(() => {
<syntaxhighlight lang="javascript">(() => {
Line 1,597: Line 1,597:
// ------------------- DOT PRODUCT -------------------
// ------------------- DOT PRODUCT -------------------


// dotProduct :: [Num] -> [Num] -> Num
// dotProduct :: [Num] -> [Num] -> Either Null Num
const dotProduct = xs =>
const dotProduct = xs =>
ys => xs.length === ys.length
ys => xs.length === ys.length
? sum(zipWith(mul)(xs)(ys))
? sum(zipWith(mul)(xs)(ys))
: undefined;
: null;