The enchanted electrical website of Dr. Sinclair
-
Functional JavaScript: What are higher-order functions, and why should anyone care?
Written by James Sinclair“Higher-order function” is one of those phrases people throw around a lot. But it’s rare for anyone to stop to explain what that means. Perhaps you already know what a higher-order function is. But how do we use them in the real world? What are some practical examples of when and how they’re useful? Can we use them for manipulating the DOM? Or, are people who use higher-order functions showing off? Are they over-complicating code for no good reason?
-
Functional JavaScript: Traversing Trees with a Recursive Reduce
Written by James SinclairTrees come up a lot in web development. As in, more than you would expect. They pop up all over the place. But trees can be tricky. If you’re like me, you know that there ought to be a way to process them neatly. You might want to change all the values, or make some calculation on the tree. But the way to do it isn’t always obvious. And utility libraries like Ramda or Lodash don’t come with tree-traversing functions. So, what do we do?
-
Magical, Mystical JavaScript Transducers
Written by James SinclairTransducers are very cool. They give us a lot of power. But they are also a bit abstract. And that makes them hard to explain. But they also embody the dream of functional programming. We write tiny, simple functions. Then we piece them together with flexible tools that work for lots of different data structures. And we end up with powerful, performant programs.
-
Functional JavaScript: Five ways to calculate an average with array reduce
Written by James SinclairThe JavaScript array reduce method seems to give people trouble. Part of the reason is that many tutorials start out using reduce only with numbers. So I wrote a previous article about the many other things you can do with reduce that don’t involve arithmetic. But what if you do need to work with numbers? How do you use reduce for real-world arithmetic problems?
-
Functional JavaScript: How to use array reduce for more than just numbers
Written by James SinclairThe array reduce method is really powerful. But people often run into trouble as soon as they step beyond the basic examples. Simple things like addition and multiplication are fine. But as soon as you try it with something more complicated, it breaks. Using it with anything other than numbers starts to get really confusing. I think this is because the examples in most tutorials mask some of what’s going on. This article looks at some of the more interesting things you can do with
.reduce()
that better show how it works. -
Elegant error handling with the JavaScript Either Monad
Written by James SinclairJavaScript gives us a built-in language feature for handling exceptions: try…catch statements. And they’re better than littering our code with if-statements. But they can be problematic. And they are not the only way to handle errors. In this article, we’ll take a look at using the ‘Either monad’ as an alternative to try…catch.
-
How to run async JavaScript functions in sequence or parallel
Written by James SinclairThe async and await keywords are a great addition to Javascript. They make it easier to read (and write) code that runs asynchronously. But they can still be confusing. Asynchronous programming is hard. Anyone who tells you differently is either lying or selling something. But there are some simple patterns you can learn that will make life easier.
-
How do you compose JavaScript functions with multiple parameters?
Written by James SinclairAs functional programmers, we like to piece our programs together out of small pieces. Our main tool for this is composition. We take an input, process it through a function, then pass it on to another function. And this all works great so long as all our functions take exactly one argument. Which never happens. So what do we do? In general, we turn to a set of tools called combinators. This article focusses on a particular combinator called lift.
-
Object destructuring and currying in functional JavaScript
Written by James SinclairCurrying is one of the most formidable weapons in our functional programming arsenal. Combined with composition, it’s extremely powerful. But is currying useful if you’re doing object destructuring with your function parameters?
-
How to deal with dirty side effects in your pure functional JavaScript
Written by James SinclairIf you start learning about functional programming, it won’t be long before you come across the idea of pure functions. And as you go on, you will discover functional programmers appear to be obsessed with them. “Pure functions let you reason about your code,” they say. “Pure functions are less likely to start a thermonuclear war.” “Pure functions giveyou referential transparency”. On and on it goes. And they have a point. Pure functions are a good thing. But what do you do with the impure bits of your code?