Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Created November 16, 2016 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daenyth/22333719e89a641d321f1770cacd6d45 to your computer and use it in GitHub Desktop.
Save Daenyth/22333719e89a641d321f1770cacd6d45 to your computer and use it in GitHub Desktop.
On Immutability

One benefit of using immutable values

When your values are immutable you are often forced to use a declarative style. This means you express what a value is, rather than imperatively building it up.

For example:

results = []
for x in values:
    results.append(func(x))

This common style of imperative code may seem simple at first, but when you read it, you as the reader must mentally construct a state machine and step by step reason about the state of mutation on each line.

Contrast with:

results = map(func, values)

The declarative style instead lets you ignore the mutation entirely and focus on what something is. Once you learn the basic abstractions that make immutability easy (map, fold, etc), you are freed from having to mentally model a computer's state machine in your head as you read code, and instead just focus on what the code means

This is the big win that FP and immutability give - freedom from mentally emulating state machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment