IntermediateTopic 38 of 104Software & Computing
Functional programming
Uses pure functions and immutability to reduce side effects.
In depth
FP treats computation as evaluating pure functions without mutable state: first-class and higher-order functions, immutability, map/filter/reduce, closures, recursion, lazy evaluation and algebraic data types. Easier reasoning and concurrency safety; languages: Haskell, Elixir, Clojure, and FP features in Python, JS and Java.
Key points
- Referential transparency
- Currying
- Side-effect isolation
Examplesum(map(lambda x: x*x, nums)) squares and sums a list.
What is Functional programming?
Uses pure functions and immutability to reduce side effects.
Why is it important to understand functional programming?
Functional programming reduces bugs by avoiding shared mutable state, which makes programs easier to test and run in parallel.
What is a pure function?
A function whose output depends only on its inputs and which has no side effects, so the same input always gives the same output.
Why does immutability help?
When data does not change, it is safe to share across threads and easier to reason about, though updates create new values.
How is Functional programming used in practice?
Typical applications include transforming a list of readings with map and filter instead of a loop, computing a result with pure functions that are easy to test and processing streams of events without changing shared variables.
What should I learn before Functional programming?
Study "Design patterns" first (topic 37 of 104 in Software & Computing). It covers the groundwork this topic builds on.
What are the key points of Functional programming?
Referential transparency. Currying. Side-effect isolation.
Can you give an example of Functional programming?
sum(map(lambda x: x*x, nums)) squares and sums a list.
What comes after Functional programming?
The next topic is "Exceptions and error handling", which builds directly on this one.
How difficult is Functional programming?
It is classed as Intermediate in Software & Computing (topic 38 of 104). It is an intermediate topic. Work through the beginner topics in Software & Computing first.