Dot product: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: Tidied the Maybe version a little) |
m (→JS ES6: Tidied) |
||
Line 1,593: | Line 1,593: | ||
<syntaxhighlight lang="javascript">(() => { |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
|||
// ------------------- DOT PRODUCT ------------------- |
|||
// dotProduct :: [Int] -> [Int] -> Int |
// dotProduct :: [Int] -> [Int] -> Int |
||
const dotProduct = |
const dotProduct = xs => |
||
ys => xs.length === ys.length |
|||
⚫ | |||
⚫ | |||
// ---------------------- 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. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
||
const zipWith = |
const zipWith = f => |
||
// A list constructed by zipping with a |
|||
const ny = ys.length; |
|||
// custom function, rather than with the |
|||
return (xs.length <= ny ? xs : xs.slice(0, ny)) |
|||
// default tuple constructor. |
|||
⚫ | |||
xs => ys => xs.map( |
|||
} |
|||
(x, i) => f(x)(ys[i]) |
|||
).slice( |
|||
⚫ | |||
); |
|||
// MAIN --- |
|||
⚫ | |||
return main(); |
|||
})();</syntaxhighlight> |
})();</syntaxhighlight> |
||
{{Out}} |
|||
<syntaxhighlight lang="javascript">3</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |