specialblend.dev

CONCEPTS OVER SYNTAX

October 30, 2024

Recall from THINKING IN EXPRESSIONS that imperative programs are built from statements.

These statements are mechanical instructions for the computer that often have nothing to do with the problem we’re trying to solve.

Let’s read this example again:

let numbers = [1, 2, 3];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
  sum += numbers[i];
}

What is the problem we’re trying to solve here?

Ultimately, we’re trying to calculate the sum of a bunch of numbers.

However, most of this program has nothing to do with the problem we’re trying to solve:

sum += numbers[i]

This is the only line in the entire program that is relevant to the problem we’re trying to solve. Everything else is just noise.

Let’s rewrite this in a functional style using these building blocks:

[1, 2, 3].reduce((a, b) => a + b, 0)

Our functional program is simple, concise, and focused on solving the problem. It focuses on what we want out of the program, rather than how to solve it mechanically.

Abstract Data Types (ADTs)

Here are some common ADTs in functional programs, agnostic of any language or syntax:

home