• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Aug 25th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. const N = 10
2.
3. // sum natural numbers from 1 to n
4. const sumNat = n => n * (n + 1) / 2
5. console.log(`sumNat(\${N}) = \${sumNat(N)}`)
6.
7. // implement iteratively
8. const sumNat1 = n => {
9.   let count = 0
10.   for (let i = 1; i <= n; i++) count += i
11.   return count
12. }
13. console.log(`sumNat1(\${N}) = \${sumNat1(N)}`)
14.
15. // implement exact algorithm recursively
16. const sumNat2 = (n, i = 1, count = 0) =>
17.   i <= n ? sumNat2(n, i + 1, count + i) : count
18. console.log(`sumNat2(\${N}) = \${sumNat2(N)}`)
19.
20. // recurse from n to 1 instead of 1 to n
21. const sumNat3 = (n, count = 0) =>
22.   n ? sumNat3(n - 1, count + n) : count
23. console.log(`sumNat3(\${N}) = \${sumNat3(N)}`)
24.
25. // recurse then add instead of add then recurse
26. const sumNat4 = n => n ? sumNat4(n - 1) + n : 0
27. console.log(`sumNat4(\${N}) = \${sumNat4(N)}`)
28.
29. // generate array from 1 to n with ugly hack
30. const range = n => [...Array(n).keys()].map(i => i + 1)
31.
32. // add all the things
33. const sum1 = ns => ns.reduce((count, n) => count + n)
34. console.log(`sum1(range(\${N})) = \${sum1(range(N))}`)
35.
36. // create reusable function
37. const add_ = (x, y) => x + y
38. const sum2 = ns => ns.reduce(add_)
39. console.log(`sum2(range(\${N})) = \${sum2(range(N))}`)
40.
41. // curry all the things
42. const reduce = combine => init => xs => {
43.   const uncurry = f => (x, y) => f(x)(y)
44.   return xs.reduce(uncurry(combine), init)
45. }
46. const add = x => (y => x + y)
47. // sum = ns => helper(ns) is just sum = helper
48. const sum3 = /* ns => */ reduce(add)(0)/* (ns) */
49. console.log(`sum3(range(\${N})) = \${sum3(range(N))}`)
50.
51. // multiply all the things
52. const mult = x => y => x * y
53. const prod1 = reduce(mult)(1)
54. // calculate the factorial
55. console.log(`prod1(range(\${N})) = \${prod1(range(N))}`)
56.
57. // generalize the pattern
58. const combineAll = ({combine, makeInit}) => {
59.   const init = makeInit()
60.   return reduce(combine)(init)
61. }
62.
63. // implement addition
64. const addMonoid = { combine: add, makeInit: () => 0 }
65. const sum4 = combineAll(addMonoid)
66. console.log(`sum4(range(\${N})) = \${sum4(range(N))}`)
67.
68. // implement multiplication
69. const multMonoid = { combine: mult, makeInit: () => 1 }
70. const prod2 = combineAll(multMonoid)
71. console.log(`prod2(range(\${N})) = \${prod2(range(N))}`)
72.
73. // implement concatenation
74. const concat = xs => ys => [...xs, ...ys]
75. const concatMonoid = { combine: concat, makeInit: () => [] }
76. const flatten = combineAll(concatMonoid)
77. const nestedArrays = [range(3), range(2), range(1)]
78. console.log(`flatten(\${
79.   JSON.stringify(nestedArrays)}) = \${
80.   JSON.stringify(flatten(nestedArrays))}`)
81.
82. /*
83.  * Tex recommends:
84.  * - drboolean.gitbooks.io/mostly-adequate-guide-old
85.  * - sanctuary.js.org
86.  * - gcanti.github.io/fp-ts
87.  * - reasonml.github.io
88.  */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top