Skip to content

Instantly share code, notes, and snippets.

View DrBoolean's full-sized avatar

Brian Lonsdorf DrBoolean

View GitHub Profile
@DrBoolean
DrBoolean / coyo_uses.js
Created February 26, 2016 15:40
Coyoneda Uses in JS
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))
}
@DrBoolean
DrBoolean / free-er.js
Last active March 17, 2024 10:33
Free(er) monads in JS (pt 1)
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
@DrBoolean
DrBoolean / monads.js
Last active June 10, 2016 01:12
monads in a minute
[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])
@DrBoolean
DrBoolean / lens10.js
Created January 21, 2016 18:16
Lens10
const arrayIso = iso(x => x.toJS(), x => List.of.apply(List, x))
@DrBoolean
DrBoolean / lens9.js
Created January 21, 2016 18:12
Lens9
// 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])
@DrBoolean
DrBoolean / lens8.js
Last active January 22, 2016 01:36
Lens8
// 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
@DrBoolean
DrBoolean / lens7.js
Created January 21, 2016 17:29
Lens7
const immLens = key => lens((x) => x.get(key), (val, x) => x.set(key, val))
@DrBoolean
DrBoolean / lens6.1.js
Created January 21, 2016 17:22
Lens6.1
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>' ]
@DrBoolean
DrBoolean / lens6.js
Last active January 24, 2016 21:58
lens6
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))
@DrBoolean
DrBoolean / lens5.js
Created January 21, 2016 16:03
Lens5
over(compose(mapped, mapped, mapped, name), toUpper, Task.of(Maybe.of([user])))
// Task(Maybe([{ id: 3, name: 'CHARLES BRONSON', addresses: [Object] }]))