DEV Community

Cover image for Pragmatic Functional Programming in JavaScript
Caleb Weeks
Caleb Weeks

Posted on • Edited on

Pragmatic Functional Programming in JavaScript

If you have been following along with my recent posts, it is no surprise that I am an avid fan of functional programming. I attempted to solve the Advent of Code 2021 problems using both Haskell and JavaScript to find an idiomatic functional solution. Unfortunately, I learned along the way that certain approaches and techniques are not practical in JavaScript. This led me to the conclusion that functional programming has different flavors, and it is important to find the right blend for your language of choice.

JavaScript is the language I use the most (whether by choice or not), and fortunately it supports functional programming fairly well. I think the bare minimum requirement needed for a language to claim to support functional programming is ergonomic support for higher order functions. (First-class functions are a requirement for higher order functions). What I mean by ergonomic support is that it has to feel natural, not gimmicky. Higher order functions work very well in JavaScript, particularly with ES6 arrow functions.

The lack of certain features such as static types (in particular algebraic data types), pattern matching, or immutable data structures limits the techniques we can use without sacrificing ergonomics, but fortunately, you can get pretty far without these features.

I came across two resources recently which have helped me think through pragmatic functional programming in JavaScript. The first is this talk by Richard Feldman that does a great job of explaining the why of functional programming. The second is this book by Kyle Simpson which proposes a flavor of functional programming that is practical in JavaScript. There are certain areas where my opinions differ from Kyle's, but this book introduces pragmatic functional programming from first principals. Also check out the Mostly Adequate Guide to Functional Programming.

Learning functional programming and figuring out how to use it practically has been a very long journey, and I think many people have felt the same frustrations that I did while learning. Although the functional programming paradigm is gaining momentum, practical resources get drowned out in the noise of both dissenters and people just riding the hype train. This is the start of a series where we will try to make sense of the plethora of information and distill it into actionable conclusions.

Top comments (0)