Map
map(f, c...)
applies function f
to each element of collection c
that enter as the the second argument of the map
function.
Below two lines of codes produce same output. This means that map
also acts as a for loop.
map(x->x+2, 1:10)
[ x + 2 for x=1:10]
Output:
10-element Vector{Int64}:
3
4
5
6
7
8
9
10
11
12
The function that enters the first argument of map
could take multiple arguments; it could be more than one argument. Notice in the below example that we have two arguments for the anonymous function (x,y)->x-2*y+2
. Then the second argument of map
should indicate the collection of possible values of x
, while the third argument of map
indicates those of y
. The anonymous function is applied element wise.
map((x,y)->x-y + 2, 1:10, -10:-1)
10-element Vector{Int64}:
13
13
13
13
13
13
13
13
13
13
Broadcasting
broadcast
, which expands singleton dimensions in array arguments to match the corresponding dimension in the other array without using extra memory, and applies the given function elementwise.
Source: https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting
a=[1,2]
b=[1 2 ; 3 4]
broadcast(+, a, b)
Output:
a
2-element Vector{Int64}:
1
2
b
2×2 Matrix{Int64}:
1 2
3 4
broadcast(+, a, b)
2×2 Matrix{Int64}:
2 3
5 6
Punctuation
Syntax | Description |
---|---|
: | Standalone : represents all indices of that dimension |
:a | Symbol (a name or a label used to identify an entity) a, often used for a dictionary key |
=> | Defines a Pair used to populate dictionaries |
-> | Used to define anonymous function |
f.() | Apply function f element-wise (broadcast ) |
Reference
https://docs.julialang.org/en/v1/base/collections/#Base.map https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting https://docs.julialang.org/en/v1/base/punctuation/