Practical introduction to functional programming

Click for: original source

Mary Rose article focusing on practical aspects of functional programming. Many functional programming articles teach abstract functional techniques. That is, composition, pipelining, higher order functions. This one is different. It shows examples of imperative, unfunctional code that people write every day and translates these examples to a functional style.

The examples are in Python, because many people find Python easy to read. A number of the examples eschew pythonicity in order to demonstrate functional techniques common to many languages: map, reduce, pipeline.

Functional code is characterised by one thing: the absence of side effects.

name_lengths = map(len, ["Mary", "Isla", "Sam"])

print name_lengths
# => [4, 4, 3]

The first section of the article takes short, data transforming loops and translates them into functional maps and reduces.

The second section takes longer loops, breaks them up into units and makes each unit functional.

The third section takes a loop that is a long series of successive data transformations and decomposes it into a functional pipeline. Nicely written!

[Read More]

Tags javascript programming web-development functional-programming