1. Home
  2. Explore
  3. Computer Science
  4. Functional Programming Skills

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Functional Programming Skills

Functional Programming as a Set of Skills

Functional programming can be understood less as a rigid definition or exclusive programming style and more as a collection of skills. Functional programmers practice certain techniques more frequently than programmers using other paradigms.

These skills are not incompatible with object-oriented or other programming approaches. For example, object-oriented programming emphasizes skills such as encapsulation and object-oriented modeling, while functional programming emphasizes different skills that can be combined with them.

The five major functional programming skills are:

  1. Discriminating between actions, calculations, and data

  2. Working effectively with immutable values

  3. Using and creating higher-order calculations and actions

  4. Modeling data effectively

  5. Applying functional architecture principles


1. Discriminating Between Actions, Calculations, and Data

The first and most fundamental skill is being able to look at code and identify whether each part represents an action, a calculation, or data.

A functional programmer should be able to distinguish actions, calculations, and data because they have different levels of predictability, testability, and difficulty.

These categories do not necessarily correspond exactly to specific language constructs. For example, calculations and actions are not always represented by functions. An arithmetic operator can also represent one of these categories.

Calculations

A calculation is a computation that maps inputs to outputs.

A calculation produces the same result whenever it receives the same inputs, regardless of when or how many times it is run. This is often associated with the idea of a pure function.

For example, a calculation that adds two numbers behaves consistently:

  • Given the same two inputs, it produces the same output.

  • It does not depend on the current time, external state, or hidden information.

  • It does not matter when the calculation is performed.

  • It can generally be repeated safely.

Calculations are usually easier to:

  • Understand

  • Test

  • Reuse

  • Compose with other calculations

  • Move to another part of a program

  • Run repeatedly without unexpected consequences

An arithmetic operator such as $+$ can be viewed as a calculation because it transforms its inputs into a predictable result.

Actions

An action is an operation whose result or effect can depend on when it is performed, how often it is performed, or the surrounding state.

Actions are sometimes called impure functions, but not every action is necessarily a function. For example, the increment operator ++ can be treated as an action because it changes state rather than merely calculating a result.

Actions can depend on:

  • The current time

  • External input

  • A database or file system

  • Mutable program state

  • Network conditions

  • The number of times the operation has already occurred

  • The order in which operations take place

The timing and repetition of actions matter. Calling an action once may produce a different overall outcome from calling it twice, while running a calculation repeatedly generally produces the same result.

Data

Data represents information rather than behavior. It can include:

  • Numbers

  • Strings

  • Records

  • Collections

  • Objects representing business facts

  • Messages or events

  • Stored information about the state of a system

Data can often be examined, copied, passed around, and transformed without immediately causing effects in the outside world.

Why the distinction matters

Calculations and data are generally easier to work with than actions. They are more independent of time and external conditions, which makes them easier to reason about.

Actions require more care because their behavior can depend on:

  • When they run

  • How often they run

  • What state exists when they run

  • What other operations have happened before them

  • Whether they succeed or fail

A useful design goal is therefore to keep the amount of code inside actions as small as possible. More of the program’s logic can be moved into calculations, where it is easier to test and reuse.

The remaining actions should receive focused attention because their placement and invocation determine the program’s observable behavior.

Practical design principle

A functional design tends to:

  1. Identify the actions in the system.

  2. Minimize the amount of logic inside those actions.

  3. Move reusable logic into calculations.

  4. Represent information as data.

  5. Carefully control when and how actions are executed.

This does not eliminate actions. Programs that interact with the real world need actions. Instead, it isolates them so the rest of the system can remain more predictable.


2. Working Effectively with Immutable Values

The second major skill is learning how to work with immutable values.

An immutable value cannot be changed after it has been created. If a program needs a different version of the value, it creates a new value rather than modifying the existing one.

Immutability in information systems

Many real-world information systems already use practices intended to preserve records:

  • Paper records may be filed away.

  • Important documents may be stored in fireproof containers.

  • Archival-quality paper and ink may be used.

  • Completed documents may be marked as final.

  • Access rules may prevent unauthorized modification.

  • Verified records may be stamped or approved so they cannot be changed.

These practices reflect the value of preserving information over time.

Computer memory, however, is usually mutable. Programs can overwrite existing values and change them directly. As a result, software developers must deliberately recreate the discipline of preserving information.

That discipline may be provided by:

  • The programming team’s coding practices

  • The programming language

  • A database system

  • Data structures designed for immutability

  • Architectural rules governing state changes

Mutable versus immutable data

With mutable data, a program can change an existing value directly:

  1. Retrieve a value.

  2. Modify it in place.

  3. Continue using the modified value.

With immutable data, the original value remains unchanged:

  1. Retrieve the original value.

  2. Create a modified copy.

  3. Use the new value while preserving the original.

The immutable approach makes changes more explicit. Instead of silently altering an existing structure, the program produces a new version.

Copying as the basic technique

Working with immutable data generally involves making copies. A new version of a value is created whenever a change is needed.

This provides several benefits:

  • Previous versions remain available.

  • Earlier states can be inspected.

  • Changes are easier to trace.

  • A value can be safely shared because another part of the program cannot unexpectedly alter it.

  • Calculations become easier to reason about because their inputs do not change behind the scenes.

The copies do not necessarily require copying every piece of data in a naive way. Efficient immutable data structures can share unchanged portions between versions. The essential idea is that the logical value is treated as unchanged, even if the implementation reuses internal pieces.

The challenge of nested data

Nested structures create one of the main difficulties of immutable programming.

Consider a company represented as a nested value:

  • A company contains employees.

  • Each employee has a salary.

  • A program needs to give every employee a raise.

  • The company, employees, and salaries are treated as immutable.

The program cannot simply change each salary inside the existing company structure. It must construct a new company value containing updated employees, while the original company remains unchanged.

A conceptual process might be:

  1. Start with the original company.

  2. Visit each employee.

  3. Create a new salary for that employee.

  4. Create a new employee containing the new salary.

  5. Create a new company containing the updated employees.

  6. Preserve the original company and employee values.

Even changing one deeply nested field requires determining which surrounding structures must be rebuilt. The deeper and more complex the structure, the more important it becomes to have techniques for updating nested immutable data.

Why this is a functional programming skill

Immutability changes the way programmers think about state:

  • State is represented by successive values rather than one value being repeatedly modified.

  • A new state is derived from an old state.

  • Previous states can remain meaningful.

  • Functions can be given stable inputs.

  • Data ownership and modification become easier to reason about.

The ability to work with immutable values is therefore more than knowing that a language has immutable types. It requires practical skill in creating updated versions of complex data, especially nested data.


3. Using and Creating Higher-Order Calculations and Actions

The third skill involves higher-order calculations and actions.

A calculation or action is higher-order when it can be treated like any other value. It can be:

  • Passed as an argument to another function

  • Returned from a function

  • Stored in a variable

  • Placed inside a data structure

  • Combined with other calculations or actions

This ability allows a program to treat behavior as data.

Four related abilities

This skill includes four related capabilities:

  1. Using higher-order calculations

  2. Using higher-order actions

  3. Creating higher-order calculations

  4. Creating higher-order actions

These are grouped together because they all depend on the ability to manipulate behavior as a value.

Passing behavior to other functions

When a function receives another function as an argument, the caller can provide behavior dynamically.

Instead of writing a separate function for every possible behavior, a general function can receive the behavior it should apply.

For example, a general processing function might receive:

  • A calculation that transforms each item

  • A condition that determines which items to keep

  • An action to perform when a particular event occurs

  • A policy for handling success or failure

This makes the surrounding function more reusable.

Returning new behavior

A function can also return another function. The returned function can capture information from the original call and apply it later.

This enables a program to create specialized behavior from general behavior. A function can act as a behavior generator or behavior transformer.

map, filter, and reduce

Common examples of higher-order functions include:

  • map: applies a transformation to each item in a collection.

  • filter: selects items that satisfy a condition.

  • reduce: combines a collection into a single result through repeated accumulation.

These functions receive other functions as arguments. Together, they support data-transformation pipelines.

A data pipeline can express a sequence such as:

  1. Select relevant records.

  2. Transform each record.

  3. Combine the transformed values.

  4. Produce a final result.

The general processing structure can be reused while the specific behavior is supplied as an argument.

Abstracting repeated action behavior

Higher-order actions allow common operational policies to be defined once and reused.

For example, an action can be wrapped in another action that:

  • Retries it three times when it fails

  • Records its execution

  • Measures its duration

  • Checks whether it is safe to repeat

  • Handles errors consistently

  • Performs cleanup after completion

Without higher-order behavior, each individual operation might need its own custom retry or error-handling code. That creates repetitive boilerplate and increases the risk of inconsistent implementations.

With higher-order actions, the general policy can be defined once and applied broadly.

Idempotence and retries

An operation is idempotent when repeating it produces the same effective result as performing it once.

Higher-order techniques can help manage operations that need to be retried. A wrapper can automatically retry an action when it fails, rather than requiring every caller to implement a separate retry loop.

However, retrying an action is safe only when its effects are understood. An action that creates a new record, charges a payment, or sends a message may produce unwanted duplicate effects if repeated. Therefore, higher-order action management must account for whether the underlying action is idempotent or can be made idempotent.

Compositional control of action sequences

Higher-order actions also make it possible to describe sequences of operations compositionally.

A program can define:

  • Which actions occur

  • How actions are combined

  • What happens when an action succeeds

  • What happens when it fails

  • Whether an action is retried

  • How errors propagate

  • How later actions depend on earlier results

This produces a more general framework than writing a single top-to-bottom sequence of statements for every case.

A mathematical example

A classic example of higher-order programming is a function that receives another mathematical function and returns a new function representing its derivative.

The general pattern is:

  • Input: a function FFF

  • Output: a new function representing the derivative of FFF

The function performing this operation does not need to know the exact formula for FFF. It operates on a function as an abstract value and produces another function.

This demonstrates the expressive power of first-class functions: the program can manipulate general behavior without knowing every detail of the behavior in advance.

Why higher-order programming is powerful

Higher-order programming supports:

  • Reuse of general algorithms

  • Separation of policy from mechanism

  • Reduction of boilerplate

  • Composition of behavior

  • Reusable error-handling and retry strategies

  • Flexible data transformation

  • Abstraction over families of related operations

It is one of the areas where the power of functional programming becomes especially visible because behavior can be manipulated in systematic and reusable ways.


4. Data Modeling

The fourth skill is data modeling: determining how to represent real-world facts, processes, and measurements in a program.

Software often automates activities that previously occurred through physical or human processes. To automate them effectively, a program must represent relevant facts and relationships as concrete data structures.

What data modeling involves

Data modeling asks questions such as:

  • What facts exist in the problem domain?

  • Which facts belong together?

  • How should relationships between facts be represented?

  • Which values are required?

  • Which values are optional?

  • How should invalid states be represented or prevented?

  • How should the data be structured for the operations the program must perform?

  • Which representation will be easiest for other systems or developers to understand?

The goal is not merely to store information. The goal is to structure information so that the program can use it effectively.

External or public-facing data modeling

One kind of data modeling concerns data that leaves a library or server. This data forms part of an API or another public interface.

Public-facing data has design constraints such as:

  • It should be understandable to other developers.

  • Its structure should be clear and predictable.

  • Its meaning should not depend on hidden internal implementation details.

  • It may need to be readable by humans as well as programs.

  • Changes to its structure may affect external users.

  • Its design should support communication between separate components.

The data model at an external boundary is therefore influenced by communication and compatibility concerns.

A public data structure might be technically efficient internally but still be a poor API if users cannot understand how to construct or interpret it.

Internal data modeling

A second kind of data modeling concerns data used inside an implementation.

Internal representations can be designed around operational needs such as:

  • Efficient search

  • Fast lookup

  • Easy traversal

  • Availability of required information

  • Efficient updates

  • Compact storage

  • Convenient access to related values

For example, a search algorithm may require a particular structure so that it can efficiently explore a space of possibilities. The internal representation does not necessarily need to be human-readable or suitable for external consumers.

External and internal models have different constraints

Public and internal data models should not automatically be treated as the same thing.

Public-facing model

  • Optimized for clarity and communication

  • Exposed through an API or system boundary

  • Constrained by compatibility requirements

  • Designed for external developers or systems

  • Should make its meaning easy to understand

Internal model

  • Optimized for implementation needs

  • Hidden within a component or system

  • May prioritize search, performance, or convenience

  • Can change without necessarily affecting external users

  • May contain structures unsuitable for public use

Separating these concerns allows each representation to serve its purpose effectively.

Data modeling as an advanced functional skill

Data modeling appears later in the progression of functional programming skills because it requires a broader understanding of:

  • The problem domain

  • Program behavior

  • Data transformations

  • System boundaries

  • Performance requirements

  • External interfaces

  • Internal implementation strategies

A programmer first needs to recognize data as distinct from actions and calculations. With more experience, the programmer can design data models that make calculations simpler and actions easier to control.


5. Functional Architecture

The fifth skill is applying functional architecture to systems that are large enough to require structure.

A small program may fit comfortably in one file without a formal architecture. As the program grows, however, simply adding more code becomes difficult to manage. A larger system needs design principles that keep it understandable and prevent it from becoming fragile.

Functional programming offers architectural patterns that emphasize controlled effects, separated responsibilities, and clear data flow.


Reactive Data Architecture

One functional architectural pattern is reactive data architecture.

This pattern is used in systems and frameworks such as:

  • Redux

  • The Elm Architecture

  • Re-frame

Although implementations differ, the general structure involves events, state, reactions, and a projected view.

Core cycle

A reactive data system generally works through a cycle:

  1. Events are generated.

  2. The system consumes those events.

  3. Event processing modifies or produces centralized state.

  4. The current state generates a view of the world.

  5. That view is projected into an interface, such as a graphical user interface.

  6. New user interactions generate additional events.

This creates a feedback cycle based on changes to data.

Centralized state

Reactive architectures often maintain some centralized representation of state. Instead of allowing many independent parts of the interface to modify one another directly, events are processed through a controlled mechanism.

The state is updated, and the view is derived from the resulting state.

This separation provides a clearer relationship between:

  • What happened

  • How the state changed

  • What the current state means

  • What the user sees

Projection onto a view

The interface is treated as a projection of application state.

Rather than manually changing every visual element in response to every event, the system can:

  1. Update the underlying state.

  2. Recalculate or derive the current view.

  3. Display the new view.

This approach emphasizes data flow and reactions to state changes instead of direct, scattered manipulation of interface elements.


Onion Architecture

Another functional architectural pattern is Onion Architecture.

The Onion Architecture organizes a system into three primary layers:

  1. Interaction layer

  2. Business rule layer

  3. Domain layer

The layers may be divided further, but the essential structure consists of these three levels.

Outer layer: interaction

The outer layer handles interaction with the outside world.

It contains the system’s actions, such as code that:

  • Receives messages

  • Communicates with external services

  • Handles API requests

  • Reads input

  • Writes output

  • Talks to databases, files, networks, or user interfaces

This layer is where the system’s effects and external dependencies are concentrated.

Because it interacts with the outside world, it is typically the layer most affected by changes to external systems.

For example, an external API may change its version or message format, requiring modifications to the interaction layer.

Middle layer: business rules

The middle layer contains the business rules. These are calculations that express changing requirements of the organization or process.

Examples include:

  • How payroll is calculated

  • How vacation allowances are determined

  • How eligibility is evaluated

  • How prices or benefits are computed

  • How an organizational policy is applied

The business rule layer should contain calculations rather than direct actions. It receives appropriate data, applies the rules, and produces results.

Business rules change over time, but usually not as frequently as external interaction details.

For example, an organization may change its vacation policy, requiring the corresponding calculation to be updated.

Inner layer: domain model

The inner layer contains the domain model: the more timeless concepts and relationships that define the problem domain.

The domain model expresses fundamental ideas such as:

  • Employees receive salaries.

  • Employees receive vacation.

  • A salary is represented as an amount.

  • Vacation is represented as an amount or allowance.

  • An employee has properties that remain meaningful regardless of temporary policy changes.

The domain model is not primarily concerned with the current version of a business policy. Instead, it captures the underlying concepts that make the business rules meaningful.

For example:

  • The rule determining how much vacation an employee receives belongs in the business rule layer.

  • The fact that employees can receive vacation belongs in the domain model.

Separation by rate of change

One of the main purposes of Onion Architecture is to separate parts of the system according to how frequently they change.

Interaction layer

  • Changes most rapidly

  • Depends on external APIs, interfaces, and protocols

  • Must adapt when external systems change

  • Contains actions and side effects

Business rule layer

  • Changes when organizational policies change

  • Contains calculations representing current requirements

  • Is less dependent on external interfaces

  • Should remain largely independent of interaction details

Domain layer

  • Changes least frequently

  • Represents fundamental concepts

  • Provides stable ideas used by business rules

  • Acts as a long-term model of the problem domain

This organization limits the impact of change. A change to an API should primarily affect the interaction layer. A change to a vacation policy should primarily affect the business rule layer. The deeper domain model should remain stable unless the underlying meaning of the domain itself changes.

Handling unusual cases

The domain model can often represent unusual or temporary situations without changing its basic structure.

For example, if a company temporarily provides no vacation or sets all salaries to zero, the model can still represent these values. A salary remains an amount, and vacation remains a concept that can have a value of zero.

This allows the system to distinguish between:

  • A change in a current policy or value

  • A change in the underlying concepts represented by the domain model


Progression of Functional Programming Skills

The five skills can be viewed as a progression from foundational reasoning to system-level design.

Foundational level: actions, calculations, and data

The first step is recognizing the different kinds of things present in code. This gives programmers a way to reason about predictability and side effects.

State-management level: immutable values

The second step is learning to preserve values rather than modifying them indiscriminately. This requires new techniques for creating updated versions of data, especially nested data.

Abstraction level: higher-order behavior

The third step involves treating calculations and actions as values. This enables reusable abstractions, compositional behavior, and generalized policies such as retries or error handling.

Representation level: data modeling

The fourth step is structuring information so that both internal algorithms and external interfaces can use it effectively.

System-design level: functional architecture

The fifth step is organizing an entire system so that actions, business rules, domain concepts, state, and external interactions remain manageable as the system grows.


Key Takeaways

Functional programming is best understood as a set of practical skills rather than a definition that must exclude other programming paradigms.

  • Actions depend on timing, state, or external effects and should be carefully controlled.

  • Calculations map inputs to outputs predictably and are easier to test, reuse, and compose.

  • Data represents information and can often be separated from behavior.

  • Immutable values preserve previous states and require new versions to be created when changes are needed.

  • Higher-order calculations and actions treat behavior as a value that can be passed, returned, stored, transformed, and composed.

  • Data modeling structures real-world facts and relationships for effective use inside and outside a system.

  • Functional architecture organizes systems around controlled effects, reactive state changes, stable domain concepts, and separated business rules.

  • Reactive architectures model applications as cycles of events, state changes, and views.

  • Onion Architecture separates rapidly changing interactions from business rules and relatively timeless domain concepts.

  • These techniques can be combined with object-oriented programming and other paradigms rather than being treated as mutually exclusive.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent