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 | var result = [] |
- Write little functional helpers, like
unary
andpartialRight
used above youself first. It is very simple, yet you will get used to writing higher-order functions that take and return other functions. For example, here are my favorite utilities; see if you can implement them.- Work through the examples in the Fun JavaScript Workshop and functional javascript workshop
- Coding these functional helpers yourself will make you a better interviewer, since they make excellent JavaScript interview questions
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 | const R = require('ramda') |
- 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.