Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/js/arrow_functions.txt

1.0 KiB, 1 run

created by r1870400018:11996, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1const compose = (...fns) =>
2 fns.reduce((f, g) => (...args) => f(g(...args)));
3
4const pipe = (...fns) =>
5 fns.reduce((f, g) => (...args) => g(f(...args)));
6
7const map = (fn) => (arr) => arr.map(fn);
8const filter = (pred) => (arr) => arr.filter(pred);
9const reduce = (fn, init) => (arr) => arr.reduce(fn, init);
10
11const sum = reduce((a, b) => a + b, 0);
12const double = map((x) => x * 2);
13const evens = filter((x) => x % 2 === 0);
14
15const sumOfDoubledEvens = pipe(evens, double, sum);
16
17const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
18console.log(sumOfDoubledEvens(numbers)); // 60
19
20const debounce = (fn, ms) => {
21 let timer = null;
22 return (...args) => {
23 clearTimeout(timer);
24 timer = setTimeout(() => fn(...args), ms);
25 };
26};
27
28const throttle = (fn, ms) => {
29 let last = 0;
30 return (...args) => {
31 const now = Date.now();
32 if (now - last >= ms) {
33 last = now;
34 fn(...args);
35 }
36 };
37};
38
39export { compose, pipe, map, filter, reduce, debounce, throttle };