An Introduction to Functional Programming in JavaScript

This is the talk I gave at the JavaScript meetup in Phoenix. One the othe largest programming meetups in the valley. Here are the slides to the talk. Here’s some books that can help you get ramped up into the functional programming paradigm Introducing functional programming with Underscore.js by Michael Fogus. And for a more in depth look into functional patterns Professor Frisby’s Mostly Adequate Guide to Functional Programming. [Read More]

bilby.js & lenses

In JavaScript nearly everything is mutable. This can cause problems in your code when you think you have a new object or variable but instead you are operating on the referenced object. So, we create patterns to alleviate this problem. Or we use libraries like underscore.js or lodash.js which incorporate the functional concepts. Unfortunately they don’t always use immutable objects either. Bilby.js solves the mutability problem by using lenses. Using the lenses pattern one can access and change one’s objects in a safe and immutable manner. [Read More]

Match Expression for JavaScript?

Sometimes (or rather most of the time) it would be nice to have more concise code in JavaScript. I was working with a function that needed quite a bit of if statements. if was awfully cluttered. I had looked at bilby.js examples and hadn’t realized that you can do multiple ternary expressions in a row. So, instead of, if (0){ return 0 } else if (1){ if (2) { return 1 } else if (3) { return 2 } else { return 3 } } else { return 4 } You can do, [Read More]

Prototypes & Compositions with Objects

I was trying to figure out the best way to have functions be both compatible with an object oriented paradigm and a functional oriented paradigm when working on objects. My implementations are probably naive, but instructive nevertheless. It seems that lo-dash and other libraries have done a good job bridging those worlds. Let’s say you have an object O such that function O(a, b){ this.a = a this.b = b } var o = function(a, b){ return new O(a, b) } Now, you want to extend the object but you also want the option to use the object in a more fuctional way with compose. [Read More]

Functional Immutability in JavaScript

This is taken from Michael Fogus’ book Functional JavaScript in the chapter Purity, Immutability, and Policies for Change. Immutability is impossible to achieve in JavaScript without using Object.freeze but it only performs makes the object immutable shallowing, you have to roll your own deepFreeze if you would like to make the object truly immutable. So there are some practices you can do to make your functions and variables perform in a more immutable manner. [Read More]

Functional or Object Oriented JavaScript?

When people are first exposed to JavaScript and its minimal set of tools (functions, objects, prototypes, and arrays), many are underwhelmed. Therefore, in order to “modify” JavaScript to conform to their idea of what it takes to model software solutions, they very often seek out or re-create class-based systems using the primordial ooze. - Michael Fogus Functional Programming Of course, the next sentence he softens that quote, I just enjoyed the first part though. [Read More]

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]

bilby.js & functional programming

I’ve been learning functional programming for quite some time and like the programming paradigm. But, it can be difficult sometimes. Some libraries like lodash.js can be easy to start learning, but others like bilby.js require that you understand more of the functional approach. So, here are some videos that have helped me understand bilby.js better. This one gives a quick overview. Note that Maybe (Haskell) is similar to Option (Scala) [Read More]