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