Notes on Monads, Monoids, and Make Believe

with Brian Lonsdorf

with Brian Lonsdorf Working with Objects the better way var SSN = Constructor(function(number, current_user){ this.number = number this.user = current_user }) SSN.prototype = { fmap: function(f){ if(this.user.is_admin) return SSN(f(this.number), this.user) } } social.fmap(replace('-', '')) //=> SSN('123456789', user) social.fmap(function(number){return number.reverse()}) //=> SSN('1234-56-789', user) the functor way Note: Functor function defined. var AdminAccess = Constructor(function(val, current_user){ this.val = val this.user = current_user }) Functor(AdminAccess, { fmap: function(f){ if(this.user.is_admin) return AdminAccess(f(this.val), this. [Read More]

Notes On Functional Programming

Patterns for the Non-Mathematician with Brian Lonsdorf

Patterns for the Non-Mathematician with Brian Lonsdorf Lenses Using lenses for composable/polymorphic model manipulation. E.g., var user = {id:1, name:{first:'doris', last:'day'}} var L = makeLenses(['name', 'first']) var firstNameChar = compose(L.name, L.first, _1) over(firstNameChar, toUpperCase, user) //=> {id:1, name:{first:'Doris', last:'day'}} Maybe Use Maybe (Haskell) types (Option types in bilby.js / Scala style) to make composition easier. This works with fmap (functor maps). Returns resultant option. Error Handling (Either) Either('need an int', 3) //=> Right(3) Either('need an int', undefined) //=> Left('need an int') fmap(function(x){return x+1;}, Right(2)) //=> Right(3) fmap(function(x){return x+1;}, Left('need an int')) //=> Left('need an int') compose(fmap(f), Either(error)) Future Values I’ll have to come back to this one later when I use it more. [Read More]