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
| interface RemoteData<T> { | |
| data: T; | |
| status: 'IDLE' | 'FETCHING' | 'SUCCESS' | 'ERROR' | |
| } | |
| const createRemoteData = <T>(data: T): RemoteData<T> => ({ | |
| data, | |
| status: 'IDLE', | |
| }); |
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
| -- Data | |
| data Shape | |
| = Rectangle Number Number -- length, height | |
| | Triangle Number Number -- base, height | |
| | Circle Number -- radius | |
| | Square Number -- length | |
| -- Behaviour | |
| area :: Shape -> Number | |
| area (Rectangle l h) = l * h |
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
| -- 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 |
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
| -- | 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 |
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 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) |
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
| 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]), |
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 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 |
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 cssColor = createMonoid({ | |
| indentity: 'transparent', | |
| semigroup(a, b) { | |
| if (a === this.identity) return b; | |
| if (b === this.identity) return a; | |
| // entah gimana implementasinya | |
| // ... | |
| 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 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 } |
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
| // 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 |