This file contains 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 daggy = require('daggy') | |
const compose = (f, g) => x => f(g(x)) | |
const id = x => x | |
//===============Define Coyoneda========= | |
const Coyoneda = daggy.tagged('x', 'f') | |
Coyoneda.prototype.map = function(f) { | |
return Coyoneda(this.x, compose(f, this.f)) | |
} |
This file contains 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 daggy = require('daggy') | |
const compose = (f, g) => x => f(g(x)) | |
const id = x => x | |
const kleisli_comp = (f, g) => x => f(x).chain(g) | |
//=============FREE============= | |
const Free = daggy.taggedSum({Impure: ['x', 'f'], Pure: ['x']}) | |
const {Impure, Pure} = Free |
This file contains 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
[1,2,3].map(x => x+1) | |
// [2, 3, 4] | |
[1,2,3].map(x => [x+1]) | |
// [[2], [3], [4]] | |
[1,2,3].chain(x => [x+1]) | |
// [2, 3, 4] | |
[1,[2],3].chain(x => [x+1]) |
This file contains 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 arrayIso = iso(x => x.toJS(), x => List.of.apply(List, x)) |
This file contains 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
// spliceAndReturn :: [a] -> [a] | |
const spliceAndReturn = xs => { | |
xs.splice(0,1) | |
return xs | |
} | |
over(arrayIso, spliceAndReturn, List.of(1,2,3,4,5)) | |
// List [2,3,4,5] | |
// | |
over(from(arrayIso), x => x.take(1), [1,2,3,4,5]) |
This file contains 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
// immutable data | |
const addrs = List.of(Map({street: '99 Walnut Dr.', zip: '04821'}), Map({street: '2321 Crane Way', zip: '08082'})) | |
const user = Map({id: 3, name: 'bob', addresses: addrs}) | |
// lenses | |
const addresses = immLens('addresses') | |
const street = immLens('street') | |
const allStreets = compose(addresses, mapped, street) | |
// getUser :: Int -> Task Error User |
This file contains 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 immLens = key => lens((x) => x.get(key), (val, x) => x.set(key, val)) |
This file contains 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 allStreets = compose(addresses, mapped, street) | |
// renderProfile :: User -> Html | |
const renderProfile = compose(map(profilePage), over(compose(mapped, allStreets), fixAddress), getUser) | |
renderProfile(1).fork(console.log, console.log) | |
[ '<span>**** Walnut Dr.<span>', '<span>**** Crane Way<span>' ] |
This file contains 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 addresses = lensProp('addresses') | |
const street = lensProp('street') | |
const allStreets = compose(addresses, mapped, street) | |
// :: Int -> Task Error User | |
const getUser = id => new Task((rej, res) => setTimeout(() => res(user), 400)) | |
// profilePage :: User -> Html | |
const profilePage = compose(map(x => `<span>${x.street}<span>`), view(addresses)) |
This file contains 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
over(compose(mapped, mapped, mapped, name), toUpper, Task.of(Maybe.of([user]))) | |
// Task(Maybe([{ id: 3, name: 'CHARLES BRONSON', addresses: [Object] }])) |