This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const array = createMonoid({ | |
| indentity: [], | |
| semigroup: (a, b) => a.concat(b), | |
| }); | |
| log(array.id) // => [] | |
| log(array.append([1], [2])) // => [1, 2] | |
| log(array.append([1, 2], array.id)) // => [1, 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const string = createMonoid({ | |
| indentity: '', | |
| semigroup: (a, b) => a + b, | |
| }); | |
| log(string.id) // => '' | |
| log(string.append('Foo', 'Bar')) // => FoorBar | |
| log(string.append('Foo', string.id)) // => Foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numMult = createMonoid({ | |
| indentity: 1, | |
| semigroup: (a, b) => a * b, | |
| }); | |
| log(numMult.id) // => 1 | |
| log(numMult.append(1, 999)) // => 999 | |
| log(numMult.append(999, numMult.id)) // => 999 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numAddition = createMonoid({ | |
| indentity: 0, | |
| semigroup: (a, b) => a + b, | |
| }); | |
| log(numAddition.id) // => 0 | |
| log(numAddition.append(1, 2)) // => 3 | |
| log(numAddition.append(3, numAddition.id)) // => 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const memoize = fn => { | |
| const cache = {} | |
| return (…args) => { | |
| const stringifiedArgs = JSON.stringify(args) | |
| const result = cache[stringifiedArgs] || fn(…args) | |
| cache[stringifiedArgs] = result | |
| return result | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const liftA2 = f => (m, n) => m.map(f).ap(n); | |
| const add = x => y => x + y; | |
| const monadicAdd = liftA2(add); | |
| add(5, 6) // 11 | |
| monadicAdd(Just(5), Just(6)) // Just(11) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const FORM_ID = 'personal-details' | |
| // Factory | |
| const onChange = (fieldName: string) => e => { | |
| notifyParentOnFieldChange(FORM_ID, fieldName) | |
| // actual computaion | |
| const value = e.target.value | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fillNames = (firstN, middleN, lastN) => | |
| firstN + middleN + lastN | |
| const a = fillNames.bind(null, 'Jihad'); | |
| const b = a(' Dzikri', ' Waspada') | |
| b === 'Jihad Dzikri Waspada' // true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const add = x => y => x + y | |
| const identity = add(0) | |
| const increment = add(1) | |
| const decrement = add(-1) | |
| identity(7) // 7 | |
| increment(7) // 8 | |
| decrement(7) // 6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fillNames = firstN => middleN => lastN => | |
| firstN + middleN + lastN | |
| const a = fillNames('Jihad'); | |
| const b = a(' Dzikri')(' Waspada') | |
| b === 'Jihad Dzikri Waspada' // true |