Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@dewey92
dewey92 / functor-naive.ts
Last active November 27, 2018 10:35
Functor in a very naive way
type Tree<A> = Leaf<A> | Branch<A>;
type Leaf<A> = { _f: 'tree', kind: 'leaf', value: A };
type Branch<A> = { _f: 'tree', kind: 'branch', left: Tree<A>, right: Tree<A> };
function mapTree<A, B>(map: (val: A) => B, tree: Tree<A>): Tree<B> {
switch (tree.kind) {
case 'leaf':
return leaf(map(tree.value));
case 'branch':
return branch(
@dewey92
dewey92 / partialApp.hs
Last active February 28, 2019 00:32
Partial App - Haskell
fillNames :: String -> String -> String -> String
fillNames firstN middleN lastN = firstN ++ middleN ++ lastN
a :: String -> String -> String
a = fillNames "Jihad"
b :: String
b = a " Dzikri" " Waspada"
b == "Jihad Dzikri Waspada" -- True
@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
@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-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 / 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-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-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 / 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 / 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