Map, Filter, Reduce: Your First Functional Toolkit in Phel
Phel, as many other functional programming languages, comes with three basic tools you should learn right from the beginning:
Map, filter and reduce.
- Map transforms one sequence into another sequence of the same length.
- Filter removes elements from a sequence depending on some predicate function.
- Reduce takes a sequence of elements and aggregates it into some value.
Let's see them in action:
The map function takes two arguments. The first argument is a one-argument function that transforms a single value. The second argument is the sequence that should be transformed.
# Increment by 1
# evaluates to [2 3 4]
# Multiply by 2 using fn syntax
# evaluates to [2 4 6]
# Multiply by 2 using fn shorthand syntax
# evaluates to [2 4 6]
The filter function takes two arguments. The first argument is a one-argument function that returns true if it should keep the value in the list. The second argument is the sequence that should be filtered.
# keep even numbers
# evaluates to [2]
# keep odd numbers
# evaluates to [1 3]
# keep number bigger 2
# evaluates to [3]
The reduce function takes three arguments. The first argument is a two-argument function (accumulated value and sequence value) that return a new accumulated value. The second argument is the initial accumulated value and the third argument is the sequence that should be reduced.
# sum all value starting by 0
# evaluates to 6
# sum all values use first value as starting point
# evaluates to 24
# concat all numbers to a string
# evaluates to "123"