官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 建德市| 崇仁县| 金川县| 油尖旺区| 同仁县| 应城市| 南和县| 吴川市| 同仁县| 长春市| 屯昌县| 宜川县| 黑龙江省| 垫江县| 留坝县| 桂平市| 仁布县| 宝兴县| 墨竹工卡县| 社旗县| 靖州| 自治县| 阳东县| 广昌县| 政和县| 新建县| 闽侯县| 乌审旗| 刚察县| 都昌县| 来安县| 清河县| 广汉市| 平原县| 灵山县| 阳曲县| 淅川县| 翼城县| 禹城市| 富裕县| 屏南县|