Map, filter, and list comprehensions

Maps and filters are typical for functional languages. A map is a function of the form map(func, coll), where func is a (often anonymous) function that is successively applied to every element of the coll collection, so map returns a new collection. Some examples are as follows:

  • map(x -> x * 10, [1, 2, 3]) returns [10, 20, 30]
  • cubes = map(x-> Base.power_by_squaring(x, 3), collect(1:5)) returns [1, 8, 27, 64, 125]
power_by_squaring is an internal function in Base, which means it is not exported, so it has to be qualified with Base.

The map function can also be used with functions that take more than one argument. In this case, it requires a collection for each argument; for example, map(*, [1, 2, 3], [4, 5, 6]) works per element and returns [4, 10, 18].

When the function passed to map requires several lines, it can be a bit unwieldy to write as an anonymous function. For instance, consider using the following function:

map( x-> begin 
if x == 0 return 0
elseif iseven(x) return 2
elseif isodd(x) return 1
end
end, collect(-3:3))

This function returns [1,2,1,0,1,2,1]. This can be simplified with a do block as follows:

map(collect(-3:3)) do x 
if x == 0 return 0
elseif iseven(x) return 2
elseif isodd(x) return 1
end
end

The do x statement creates an anonymous function with the argument x and passes it as the first argument to map.

A filter is a function of the form filter(func, coll), where func is a (often anonymous) Boolean function that is checked on each element of the collection coll. Filter returns a new collection with only the elements on which func is evaluated to be true. For example, the following code filters the even numbers and returns [2, 4, 6, 8, 10]:

filter( n -> iseven(n), collect(1:10))

An incredibly powerful and simple way to create an array is to use a list comprehension. This is a kind of implicit loop which creates the result array and fills it with values. Some examples are as follows:

  • arr = Float64[x^2 for x in 1:4] creates 4-element Array{Float64,1} with elements 1.0, 4.0, 9.0, and 16.0.
  • cubes = [x^3 for x in collect(1:5)] returns [1, 8, 27, 64, 125].
  • mat1 = [x + y for x in 1:2, y in 1:3] creates a 2 x 3 array (Array{Int64,2}):
2  3  4
3 4 5
  • table10 = [x * y for x=1:10, y=1:10] creates a 10 x 10 array (Array{Int64,2}), and returns the multiplication table of 10.
  • arrany = Any[i * 2 for i in 1:5] creates 5-element Array{Any,1} with elements 2, 4, 6, 8, and 10.

For more examples, you can refer to the Dictionaries section in Chapter 5, Collection Types.

Constraining the type, as with arr, is often helpful for performance. Using typed comprehensions everywhere for explicitness and safety in production code is certainly the best practice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.239.226