Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@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 / 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 / bool-monoid.js
Last active April 4, 2019 07:52
Boolean Monoid
// OR
const boolOr = createMonoid({
indentity: false,
semigroup: (a, b) => a || b,
});
log(boolOr.id) // => false
log(boolOr.append(true, boolOr.id)) // => true
log(boolOr.append(false, boolOr.id)) // => false
@dewey92
dewey92 / object-monoid.js
Created April 4, 2019 01:15
Object Monoid
const obj = createMonoid({
indentity: {},
semigroup: (a, b) => { ...a, ...b }, // Object.assign({}, a, b)
});
log(obj.id) // => {}
log(obj.append({ a: 1 }, { b: 2 })) // => { a: 1, b: 2 }
log(obj.append({ a: 1 }, obj.id)) // => { a: 1 }
@dewey92
dewey92 / css-monoid.js
Last active April 4, 2019 08:08
CSS Monoid
const cssColor = createMonoid({
indentity: 'transparent',
semigroup(a, b) {
if (a === this.identity) return b;
if (b === this.identity) return a;
// entah gimana implementasinya
// ...
return result;
@dewey92
dewey92 / endo-func.js
Last active April 4, 2019 10:25
Endomorphism Monoid
const endoFunc = createMonoid({
indentity: x => x,
semigroup: compose,
});
const incr = x => x + 1;
const square = x => x * x;
log(endoFunc.id) // x => x
log(endoFunc.append(square, incr)(9)) // => 100
@dewey92
dewey92 / jsx-monoid.js
Last active April 4, 2019 08:58
JSX Monoid
import React from 'react';
const fragment = children => React.createElement.apply(
null,
[React.Fragment, {}].concat(children)
);
const jsx = createMonoid({
indentity: null,
semigroup: (a, b) => fragment([a, b]),
@dewey92
dewey92 / func-monoid.js
Last active April 4, 2019 11:08
Func Monoid
const func = createMonoid({
indentity: x => x,
// The code below can't run but conceptually valid
// In Haskell or Purescript, it's easier to write Monoid instance
semigroup: (f, g) => x => f(x).append(f(g)),
});
// toStr :: Int -> String
const toStr = x => parseInt(x)
@dewey92
dewey92 / haskell-monoid.hs
Last active April 6, 2019 09:41
Haskell Monoid
-- | Instance Array
-- | String is in fact an Array of Char
instance Semigroup [a] where
(<>) = (++)
instance Monoid [a] where
mempty = []
-- | Instance Num under addition
newtype Sum n = Sum n
instance Num n => Semigroup (Sum n) where
@dewey92
dewey92 / basic-adt-haskell.hs
Created May 2, 2019 17:33
Basic Haskell ADT
-- Boolean teridiri dari dua buah value: True dan False
data Bool = True | False
-- List (atau Array) bisa diekspresikan dengan 2 buah data: Nil dan Cons.
-- @example
-- ["Jihad", "Waspada"] == Cons "Jihad" (Cons "Waspada" Nil)
data List a = Nil | Cons a (List a)
-- Beberapa Functor..
data Maybe = Just a | Nothing