Functional programming explained: pure functions, immutable data and small functions chained with map, filter and reduce, and why they make code predictable.
Functional Programming, explained by cats. Kitty writes code. But Kitty hates surprises. In normal code, things change. Variables get updated. State moves around. Hard to track. So Kitty tries Functional Programming. In functional programming, functions behave like well-trained cats. Same input.
always produces the Same output. Every time. Instead of modifying data, those functions return new data. Old data stays untouched. Functions don’t depend on the outside world. This makes code easier to test. Easier to reason about. and Easier to run in parallel. Functional programming isn’t about style. It’s about predictability. That’s Functional Programming. no side effects, no surprises, just clean transformations.
Functional programming is a way of writing code where a function takes input, returns output and changes nothing else along the way.
What you get for it is predictability: when a function can't reach outside itself, you can read it, test it and run it in parallel without wondering what else it might break.
Where the surprises come from
Most code people learn first is imperative: a list of steps that update variables as they go. That works, but once several parts of a program read and write the same variable, its value at any moment depends on everything that ran before.
The same call with the same argument gave two different answers. The function looks harmless, but its result hangs on a variable any line in the program can change. When the number is wrong, you have to find every place that writes to . This is what 'state moving around' means, and it's why bugs like this are hard to track.
Functional Programming, explained by cats. Kitty writes code. But Kitty hates surprises. In normal code, things change. Variables get updated. State moves around. Hard to track. So Kitty tries Functional Programming. In functional programming, functions behave like well-trained cats. Same input.
always produces the Same output. Every time. Instead of modifying data, those functions return new data. Old data stays untouched. Functions don’t depend on the outside world. This makes code easier to test. Easier to reason about. and Easier to run in parallel. Functional programming isn’t about style. It’s about predictability. That’s Functional Programming. no side effects, no surprises, just clean transformations.
Functional programming builds on pure functions, which follow two rules:
Same input, same output: call it with the same arguments and you always get the same result.
No side effects: it does nothing except return that result.
A side effect is anything a function does besides returning a value: changing a variable outside itself, changing an object it was passed, writing to a file or database, printing to the console, or reading something that changes on its own, such as the clock or a random number.
The discount function becomes pure once everything it needs comes in through its arguments:
function priceAfterDiscount(price, discount) { return price * (1 - discount);}
Now priceAfterDiscount(100, 0.1) is 90 today, tomorrow and on every machine. You could replace the call with 90 and the program would behave the same. That property has a name, referential transparency, and it's what lets you reason about a piece of code on its own, without holding the whole program in your head.
Why pure functions are easy to test
A test for a pure function is a call and an expected value. You don't set up globals beforehand or fake a database or network. The tests can run in any order, because none of them leaves anything behind for the next one to trip over.
Make new data instead of changing old data
The second idea is immutability: once a value is created, you don't change it. When you need something different, you build a new value and leave the old one alone.
Here is a function that quietly changes what it was given:
function addTag(post, tag) { post.tags.push(tag); // edits the caller's post return post;}
Anyone else holding that post now sees the new tag too, whether they expected it or not. The immutable version returns a fresh object:
The caller's original post is untouched, so any code still using it gets exactly what it had before. The spread syntax (...) copies only one level deep, so nested objects are still shared. That's fine as long as nobody changes them.
Copying sounds wasteful, and for large data in a tight loop it can be. Languages built around immutability, such as Clojure, Haskell and Elixir, use persistent data structures that share the unchanged parts between the old and new versions, so a 'copy' costs far less than it sounds.
Small functions, chained together
Functions in a functional style are values: you can pass one to another function or return one from it. A function that takes or returns another function is a higher-order function, and three of them do most of the everyday work:
let total = 0;for (const o of orders) { if (o.paid) { total += o.price; }}
Both are correct. The chain describes the result instead of spelling out how to build it, and none of its steps changes orders. Each step is a small function you could pull out, name and test on its own. Python has the same tools, and its comprehensions are often the clearest way to write them:
total = sum(o["price"] for o in orders if o["paid"])
Why it helps with parallel code
Two tasks running at the same time only get in each other's way when they share something they can both change. That collision is a race condition, and it's one of the hardest kinds of bug to reproduce. With pure functions and immutable data there's nothing shared to fight over.
It also makes work easy to split. When map applies a pure function to each item, no item depends on another, so the list can be divided across several cores or machines and the results put back together. Google's MapReduce system borrowed map and reduce from functional languages to split work across many machines in this way.
Side effects don't go away
A program that never writes to a screen, a file or a network is useless. Functional programming keeps side effects, but only in a few known places. A common shape is a functional core with an imperative shell: the outer layer reads the input and writes the output, and everything in between is pure functions that turn one value into another.
All the logic worth testing lives in buildReport, and the effects sit at the edges where you can see them. Passing in what a function needs, rather than letting it reach for globals, is the same idea that drives dependency injection.
Functional languages and the functional style
Some languages are built around these ideas: Haskell is purely functional, and Clojure, Elixir, F# and Elm all lean that way. But you don't need to switch language to use them. JavaScript, Python, C#, Kotlin and Java all have lambdas, map and filter, and ways to make data that can't be changed. React asks for the same discipline: its components are meant to be pure functions of their props and state.
This is what the video means by predictability rather than style. Pure functions, immutable data and side effects kept at the edges make code easier to predict in almost any language.
When not to push it
Hot loops over large data: copying a big array on every step costs memory and time. Changing a local variable inside a function is fine as long as nothing outside can see it: from the outside, the function is still pure.
Deep recursion in JavaScript: functional languages often use recursion where others use loops, but most JavaScript engines don't optimise it, so a deeply recursive version can overflow the stack where a loop would not.
Clever chains: ten chained steps with one-letter names can be harder to read than a plain loop. Name the steps, or use the loop.
Common mistakes
Thinking const makes data immutable: it stops you reassigning the variable, but you can still change the object or array it points to.
Sorting in place by accident: in JavaScript, arr.sort() changes arr itself. Copy first with [...arr].sort(), or use arr.toSorted().
Hidden impurity: a function that calls Date.now() or Math.random() gives a different answer each time. Pass the time or the random value in as an argument instead.
Using map for side effects: if you only want to do something for each item and don't need the new array, use a plain loop or forEach.
Key takeaways
A pure function returns the same output for the same input and changes nothing outside itself.
Make new data instead of changing old data; const on its own doesn't do that.
Chain small functions such as filter, map and reduce, so each step is easy to test.
Without shared state that can change, there are fewer surprises in tests and in parallel code.
Keep side effects at the edges; you can write in a functional style in most languages.