Getting good at FP

How to start and get better at using JavaScript in functional way.

If you want to get better at using functional programming in JavaScript

  • You need to master the language itself, I suggest starting by reading a good beginner book from this list.
  • Read the advanced books "JavaScript Allongé" and "Functional JavaScript"
  • While reading "JavaScript Allonge", apply the examples to your own code. The goal is to increase code's readability and testability.
    • Find simple cases where a single function does "too much" and refactor it. For example, if there is a function that converts a list of strings into numbers with the base 10, it could be refactored in steps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var result = []
['100', '101', '102'].forEach(function (str) {
result.push(parseInt(str, 10))
})
// to
const result = ['100', '101', '102'].map(function (str) {
return parseInt(str, 10)
})
// to
const partialRight = (fn, b) => (a) => fn(a, b)
const result = ['100', '101', '102'].map(partialRight(parseInt, 10))
// or
const unary = (fn) => (x) => fn(x)
const result = ['100', '101', '102'].map(unary(parseInt))

As you get more comfortable with these transformations, the amount of functional code in your programs will slowly grow.

  • Try to decrease the number of impure functions that use outside state. Some functions have to perform "dirty" tasks, but majority of data transformations can be made pure. Pure functions are simpler to understand, test and reuse.
  • Become familiar with large libraries of little utility functions, like Ramda and Lodash - most of my own code is based on reusing functions from these libraries. For example, I do not use my own implementation of unary, but rely on Ramda's
1
2
3
4
5
6
7
8
const R = require('ramda')
const parse = R.unary(parseInt)
const result = ['100', '101', '102'].map(parse)
// to
const R = require('ramda')
const parse = R.unary(parseInt)
const toInts = R.map(parse)
const result = toInts(['100', '101', '102'])
  • Get comfortable working with wrapped values instead of "primitive" objects. Such wrapped values have fancy names like Functors and Monads, but just think about them as values with extra logic for applying functions to the wrapped value. For example this tutorial gives good examples replacing if (value === null) statements with cleaner functional code.
  • Read the examples and tutorials on functional concepts, there is a huge list of resources. Do not limit yourself to just JavaScript - there are great explanations based on other languages. For example Scott Wlaschin uses F# yet his examples are super useful to any programmer.
  • Read my functional JavaScript programming blog posts
  • Browse my functional presentations slides for quick examples
  • Join the Ramda gitter channel gitter.im/ramda/ramda where any functional question is welcome and answered pretty quickly.
  • Apply functional programming practice to reactive programming, for examples read MVC to FRP blog post.
  • If your coworkers object to using functional solutions, you need a strategy to overcome their objections
  • Review the Functional Programming Jargon
  • Read whatever catches your eye in the very very long Awesome Functional Programming list on GitHub.