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 Right = value => ({ | |
| map: f => Right(f(value)), // Either[E,A].map(fn: A => B): Either[E,B] | |
| bind(f) { return this.map(f).join() }, // Either[E,A].bind(fn: A => Either[E,B]): Either[E,B] | |
| join: () => value, | |
| isRight: () => true, | |
| isLeft: () => false, | |
| cata: (l, r) => () => r(value) | |
| }) | |
| const Left = 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 validateLogin = auth => { | |
| const checkUsername = auth => auth.username | |
| ? Right(auth) | |
| : Left('No Username') | |
| const checkPassword = auth => auth.password | |
| ? Right(auth) | |
| : Left('Password Empty') | |
| const checkMatch = auth => (auth.username === 'jihad' && auth.password === 'jihad123') | |
| ? Right(auth.username) | |
| : Left('Username && password mismatch') |
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
| // form/utils.js | |
| const createEitherFromConstraints = (obj, cons) => cons.reduce((acc, con) => acc.bind(con), Either.of(obj)) | |
| // module/auth/constraints.js | |
| const checkUsername = auth => auth.username | |
| ? Right(auth) | |
| : Left('No Username') | |
| const checkPassword = auth => auth.password | |
| ? Right(auth) |
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 incr = x => x + 1 | |
| const add = curry((x, y) => x + y) | |
| const sum3 = curry((x, y, z) => x + y + z) | |
| liftA(incr, Functor1) === pure(incr).ap(Functor1) | |
| liftA2(add, Functor1, Functor2) === pure(add).ap(Functor1).ap(Functor2) | |
| liftA3(sum3, Functor1, Functor2, Functor3) === pure(sum3).ap(Functor1).ap(Functor2).ap(Functor3) |
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
| /* Cara 1: dengan Higher Order Function */ | |
| const add = x => y => x + y | |
| const add1 = add(1) // y => 1 + y | |
| console.log(add1(3)) | |
| // => 4 | |
| /* Cara 2: pakai library biar gampang (Lodash, Ramda, etc) */ | |
| import { curry } from 'ramda' |
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 getChild = person => Maybe(person.child) | |
| const getName = person => person.name | |
| const textPenghulu = curry((co, ce) => `Saya nikahkan ${co} dan ${ce}, sah!`) | |
| const jihad = getName(getChild(mnunirFam)) | |
| const pita = getName(getChild(wawanFam)) | |
| console.log(textPenghulu(jihad, pita) | |
| // Result: "Saya nikahkan undefined dan undefined, sah!" | |
| // Expected: "Saya nikahkan Jihad dan Puspita, sah!" |
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 incr = x => x + 1 | |
| const add = curry((x, y)) => x + y | |
| const sum3 = curry((x, y, z)) => x + y + z | |
| const maybe1 = Maybe(1) | |
| const maybe2 = Maybe(2) | |
| const maybe3 = Maybe(3) | |
| /* Pada dasarnya, liftA merupakan kombinasi dari map dan ap */ |
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 jihad = liftA(getName, getChild(munirFam)) // Maybe('Jihad') | |
| const pita = liftA(getName, getChild(wawanFam)) // Maybe('Puspita') | |
| console.log(liftA2(textPenghulu, jihad, pita)) | |
| // => Maybe('Saya nikahkan Jihad dan Puspita, sah!') |
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
| // add :: Number β Number β Number | |
| const add = (x, y) => x + y | |
| // mathMsg :: (Number β Number β Number) β Number β Number β String | |
| const mathMsg = (mathOp, x, y) => { | |
| const result = mathOp(x, y) | |
| return `The result of ${x} and ${y} is ${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
| // map :: (a β b) β [a] β [b] | |
| const map = fn => arr => arr.map(fn) | |
| // :: (Int β String) β [Int] β [String] | |
| map(x => x.toString())([1, 2, 3]) | |
| # ['1', '2', '3'] | |
| // :: (Int β Int) β [Int] β [Int] | |
| map(x => x + 1)([1, 2, 3]) |