Functional programming and composable pipelines
A major goal of functional programming is functional purity. This means that functions should be implemented such that they have no side effects. A pure function does not modify any global state or mutate any data structures in place. Take the example of appending an item to a list. In C, it might look like this:
list_append(list, item);
This will add item
to list
in place. When list_append
returns, the state of list
has been mutated. This is a side effect, making it an impure function. In a functional language, it would look more like this:
list2 = list_append(list1, item);
In this form, list1
is immutable; new items cannot be added or inserted. Instead, this function returns a new list, list2
, containing all of the elements of list1
followed by the new addition, item
. Immutability coupled with purity can make it easier to reason about a program's behavior. It also leads naturally to functions which are self-contained, tightly-sealed units, with...