Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@dewey92
dewey92 / array-monoid.js
Last active April 4, 2019 01:00
Array Monoid
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]
@dewey92
dewey92 / string-monoid.js
Last active April 4, 2019 01:00
String Monoid
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
@dewey92
dewey92 / num-mult-monoid.js
Created April 4, 2019 00:49
Num Mult Monoid
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
@dewey92
dewey92 / num-add-monoid.js
Last active April 4, 2019 00:51
Num Add Monoid
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
@dewey92
dewey92 / partialApp-cache.js
Created February 28, 2019 02:38
Partial App - Cache - JS
const memoize = fn => {
const cache = {}
return (…args) => {
const stringifiedArgs = JSON.stringify(args)
const result = cache[stringifiedArgs] || fn(…args)
cache[stringifiedArgs] = result
return result
}
}
@dewey92
dewey92 / partialApp-af.js
Last active February 28, 2019 02:16
Partial App - Applicative Functor JS
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)
@dewey92
dewey92 / partialApp-event.tsx
Last active February 28, 2019 01:44
Partial App - Events
const FORM_ID = 'personal-details'
// Factory
const onChange = (fieldName: string) => e => {
notifyParentOnFieldChange(FORM_ID, fieldName)
// actual computaion
const value = e.target.value
...
}
@dewey92
dewey92 / partialApp-2.js
Created February 28, 2019 00:29
Partial App bind - JS
const fillNames = (firstN, middleN, lastN) =>
firstN + middleN + lastN
const a = fillNames.bind(null, 'Jihad');
const b = a(' Dzikri', ' Waspada')
b === 'Jihad Dzikri Waspada' // true
@dewey92
dewey92 / factory-func.js
Last active February 27, 2019 22:56
Factory Fn - JS
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
@dewey92
dewey92 / partialApp.js
Last active February 28, 2019 00:31
Partial App - JS
const fillNames = firstN => middleN => lastN =>
firstN + middleN + lastN
const a = fillNames('Jihad');
const b = a(' Dzikri')(' Waspada')
b === 'Jihad Dzikri Waspada' // true