#MonthOfJulia Day 7: Functional Programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming. Wikipedia: Functional Programming
Functional Programming is characterised by higher order functions which accept other functions as arguments. Typically a Functional Programming language has facilities for anonymous “lambda” functions and ways to apply map, reduce and filter operations. Julia ticks these boxes.
We’ve seen anonymous functions before, but here’s a quick reminder of the syntax:
Let’s start with
map() which takes a function as its first argument followed by one or more collections. The function is then mapped onto each element of the collections. The first example below applies an anonymous function which squares its argument.
The analogues for this operation in Python and R are
filter(), as its name would suggest, filters out elements from a collection for which a specific function evaluates to true. In the example below the function
isprime() is applied to integers between 1 and 50 and only the prime numbers in that range are returned.
The equivalent operation in Python and R is carried out using
The fold operation is implemented by
reduce() which builds up its result by applying a bivariate function across a collection of objects and using the result of the previous operation as one of the arguments. Hmmmm. That’s a rather convoluted definition. Hopefully the link and examples below will illustrate. The related functions,
foldr(), are explicit about the order in which their arguments are associated.
The fold operation is applied with
Reduce() in Python and R respectively.
Finally there’s a shortcut to achieve both map and reduce together.
A few extra bits and pieces about Functional Programming with Julia can be found on github.