Question about square brackets with for and f.() calls

Hi there! Who can explain, how it works?
A = [i + 3*j for i=0:2, j=1:3]
f.(A)
f.(A)

https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions

https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting

3 Likes

Welcome @Volodymyr_Pakholok !

You see here what is called a comprehension:

[i + 3*j for i=0:2, j=1:3]

https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions

It creates an array, here a 2 dimensional Array, because of two index variables i and j. An easier example in the 1-dim case would be:

julia> [ i+1 for i=0:2 ]
3-element Array{Int64,1}:
 1
 2
 3

Than

f.(A)

is a function call for every element of your Array A, means: for every element in A the function f is called and a new Array of equal size and dimension with the result values is returned.

It’s called Dot-syntax:
https://docs.julialang.org/en/v1/manual/functions/#man-vectorized
which is equivalent broadcasting:
https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting
(edited cause of ninja’d by @Jeff_Emanuel :slight_smile: )

3 Likes