Functional Programming Skills


Functional Programming Skills

Hope
When people ask, “What is functional programming?” they often get different, sometimes contradictory answers. A useful way to approach it is to stop arguing over one perfect definition and instead think of functional programming as a set of skills—skills that functional programmers tend to use more often than people working in other paradigms.

Michael
And that does not make functional programming incompatible with object-oriented programming. Object-oriented programming also involves skills, such as encapsulation and object-oriented modeling. You can combine those skills with functional ones, choosing the techniques that help you solve a particular problem.

Hope
The first skill is distinguishing among actions, calculations, and data. The names are less important than being able to look at code and recognize which category each part belongs to.

Michael
A calculation is a computation from inputs to outputs. If you provide the same arguments, you get the same answer regardless of when you run it; these are often called pure functions, although calculations are not always functions in every language. An action, by contrast, can produce different results depending on when it runs, and actions are not necessarily functions either.

Hope
Operators can make the distinction clearer. The plus operator is a calculation, but an increment operator such as plus-plus is an action, because it changes something. Being able to spot that difference matters because calculations and data are easier to test and reuse: you do not have to worry as much about when they run.

Michael
Actions are harder because timing, frequency, and ordering matter. So a functional programmer generally tries to keep the action code smaller, move more logic into calculations, and pay extra attention to how actions are called, when they are called, and how many times they are called.

Hope
The second skill is working effectively with immutable values. In the physical world, we already use systems for preserving records: filing documents, storing them in fireproof boxes, using archival materials, or marking a record as checked so it cannot be changed.

Michael
Computer memory, however, is naturally mutable, so software developers have to deliberately rebuild that discipline. Immutability might be enforced by the programming team, by the language, or by the database, but it has to exist somewhere. Working immutably is not the same as freely overwriting data; it usually means creating new copies rather than changing the original.

Hope
Nested data is one of the bigger challenges. Imagine an immutable company containing immutable employees, each with an immutable salary. If one employee gets a raise—or everyone gets a ten percent raise—you need to work out which parts of the nested structure must be copied to represent the new company while preserving the old one.

Michael
That leads to the third skill: using and creating higher-order calculations and actions. A higher-order calculation or action can be treated like any other value. You can pass it as an argument, return it from another function, or store it in a variable.

Hope
There are really four related abilities here: using higher-order calculations, using higher-order actions, creating higher-order calculations, and creating higher-order actions. Together, they let you pass behavior around and manipulate it in regular, reusable ways.

Michael
For example, you could take an action and automatically make it retry three times if it fails, or make it idempotent. Without higher-order behavior, you might have to write a special retry program every time. With it, you can define that behavior once and apply it consistently.

Hope
This is also the idea behind map, filter, and reduce. They take functions and let you build data-transformation pipelines. Higher-order actions can similarly describe sequences of actions, including what happens when something fails, in a compositional way rather than relying only on straight top-down code.

Michael
A classic example comes from Structure and Interpretation of Computer Programs: writing a function that accepts another function, F, and returns a new function that calculates F’s derivative. The original function could be any mathematical function. That kind of general behavior is possible because functions can be treated as first-class values.

Hope
The fourth skill is data modeling. Software takes facts and measurements about real-world situations and processes, then combines them into concrete structures that the program can use. Data modeling is the skill of deciding how to structure that information effectively.

Michael
There are at least two important kinds of data modeling. Public-facing data—such as data leaving a library or server as part of an API—needs to be understandable to the people or programs on the other side. Internal data modeling has different constraints: for example, a search system may need a representation that makes searching efficient and keeps the necessary information available.

Hope
The fifth skill is functional architecture. A small program might fit comfortably in one file, but as a system grows, it needs principles that keep it manageable. Functional programming offers architectural patterns, including reactive data architectures and the Onion Architecture.

Michael
In a reactive data architecture, events are generated and consumed, which changes some centralized state. That state produces a view of the world, and the view is projected outward—for example, onto a graphical user interface. Redux, the Elm Architecture, and re-frame are examples associated with this kind of pattern.

Hope
The Onion Architecture has three main layers. The outer interaction layer contains the actions that communicate with the outside world and receive external messages. It calls into the business-rule layer, which is made up of calculations and contains changing policies, such as how payroll or vacation is calculated.

Michael
Inside that is the domain layer, which contains a more timeless domain model. A business rule might determine how much vacation an employee receives, while the domain model captures the more stable idea that employees receive vacation at all. The layers can be divided further, but those three layers are the central pattern.

Hope
The separation reflects different rates of change. Interaction code often changes fastest because external APIs and connections change. Business rules change too, but usually less rapidly. The domain model changes least often, and even unusual cases—such as everyone temporarily receiving zero vacation or having salaries of zero—can still be represented within that model.

Michael
So the five major skills are, first, distinguishing actions, calculations, and data; second, working with immutable values; third, using and creating higher-order calculations and actions; fourth, modeling data; and fifth, designing functional architecture.

Hope
Those are not the only possible functional-programming skills, but they are five major ones, moving from foundational distinctions toward more advanced modeling and architecture. The central message is that functional programming is best understood not as a single rigid definition, but as a practical collection of skills you can develop and combine with other programming approaches.

Ask anything
We've read this whole study set, so questions about it get the best answers.