Tag Archives: map

#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:

julia> x -> x^2
(anonymous function)

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.

julia> map(x -> x^2, [1:5])
5-element Array{Int64,1}:
  1
  4
  9
 16
 25
julia> map(/, [16, 9, 4], [8, 3, 2])
3-element Array{Float64,1}:
 2.0
 3.0
 2.0

The analogues for this operation in Python and R are map() and mapply() or Map() respectively.

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.

julia> filter(isprime, [1:50])
15-element Array{Int64,1}:
  2
  3
  5
  7
 11
 13
 17
 19
 23
 29
 31
 37
 41
 43
 47

The equivalent operation in Python and R is carried out using filter() and Filter() respectively.

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, foldl() and foldr(), are explicit about the order in which their arguments are associated.

julia> reduce(/, 1:4)
0.041666666666666664
julia> ((1 / 2) / 3) / 4
0.041666666666666664

The fold operation is applied with reduce() and Reduce() in Python and R respectively.

Finally there’s a shortcut to achieve both map and reduce together.

julia> mapreduce(x -> x^2, +, [1:5])
55
julia> (((1^2 + 2^2) + 3^2) + 4^2) + 5^2
55

A few extra bits and pieces about Functional Programming with Julia can be found on github.

The post #MonthOfJulia Day 7: Functional Programming appeared first on Exegetic Analytics.

Element-wise mathematical operators and iterator slides

By: Christian Groll

Re-posted from: http://grollchristian.wordpress.com/2014/08/06/iterators-and-comprehensions-slides/

I recently did engage in a quite elaborate discussion on the julia-stats mailing list about mathematical operators for DataFrames in Julia. Although I still do not agree with all of the arguments that were stated (at least not yet), I did get a very comforting feeling about the lively and engaged Julia community once again. Even one of the most active and busiest community members, John Myles White, did take the time to elaborately explain his point of view in the discussion – and this just might be the even higher good to me. Different opinions will always be part of any community. But it is the transparency of the discussions that tell you how strong a community is.

Still, however, mathematical operators are important to me, as I am quite frequently working with strictly real numeric data: no Strings, and no columns of categorical IDs. Given Julia’s expressive language, it would be quite easy to implement any desired mathematical operators for DataFrames on my own. However, I decided to follow what seems to be the consensus of the DataFrame developers, and hence refrain from any individual deviations in this direction. Alternatively, I decided to simply relate any element-wise operators of multi-column DataFrames to DataArray arithmetic, which allow most mathematical operators for individual columns. Viewed from this perspective, element-wise DataFrame operators are nothing else than operators that are successively applied to individual columns of a DataFrame, which are DataArrays.

As a consequence of this, I had to deepen my understanding of iterators, comprehensions and functions like vcat, map and reduce. For future reference, I did sum up my insights in a slide deck, which anybody who is interested could find here, or as part of my IJulia notebook collection here.

For those of you who are using the TimeData package, the current road-map regarding mathematical operators will be the following: any types that are constrained to numeric values only (including the extension to NA values) will carry on providing mathematical operators. These operators do perform some minimal checks upfront, in order to minimize risk of meaningless applications (for example, only adding up columns with equal names, equal dates,…). Furthermore, for any type that allows values other than numeric data these mathematical operators will not be defined. Hence, anybody in need of element-wise arithmetic for numeric data could easily make use of either Timematr or Timenum types (even if you do not need any time index). If you do, however, make sure to not mix up real numeric data and categorical data: applying mathematical operators or statistical functions like mean to something like customer IDs most likely will lead to meaningless results.

Filed under: Julia Tagged: iterators, map, slides