RosettaCodeData/Task/Factorial/JavaScript/factorial-7.js

29 lines
549 B
JavaScript
Raw Permalink Normal View History

2018-06-22 20:57:24 +00:00
(() => {
2016-12-05 22:15:40 +01:00
'use strict';
// factorial :: Int -> Int
2018-06-22 20:57:24 +00:00
const factorial = n =>
enumFromTo(1, n)
.reduce(product, 1);
2016-12-05 22:15:40 +01:00
2018-06-22 20:57:24 +00:00
const test = () =>
factorial(18);
// --> 6402373705728000
2016-12-05 22:15:40 +01:00
2018-06-22 20:57:24 +00:00
// GENERIC FUNCTIONS ----------------------------------
2016-12-05 22:15:40 +01:00
2018-06-22 20:57:24 +00:00
// product :: Num -> Num -> Num
const product = (a, b) => a * b;
2016-12-05 22:15:40 +01:00
2018-06-22 20:57:24 +00:00
// range :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
2016-12-05 22:15:40 +01:00
2018-06-22 20:57:24 +00:00
// MAIN ------
return test();
})();