map()
是 julia 中的一个内置函数,用于通过对每个元素使用指定的操作来转换指定的集合。
Syntax: map(f, c…)
Parameters:
- f: Specified operation.
- c: Specified collection.
Returns: It returns the transformed elements.
例子:
# Julia program to illustrate
# the use of map() method
# Getting the transformed elements.
println(map(x -> x * 2, [5, 10, 15]))
println(map(+, [1, 2, 3], [10, 20, 30]))
println(map(x -> x / 5, [5, 10, 15]))
println(map(-, [10, 20, 30], [1, 2, 3]))
输出:
[10, 20, 30]
[11, 22, 33]
[1.0, 2.0, 3.0]
[9, 18, 27]
地图!()
map!()
是 julia 中的一个内置函数。此函数与 map()函数相同,但将结果存储在目标中而不是新集合中。目标必须至少与第一个集合一样大。
Syntax:
map!(function, destination, collection…)
Parameters:
- function: Specified operation.
- destination: The destination where the result get stored.
- collection: Specified collection.
Returns: It returns the transformed elements.
例子:
# Julia program to illustrate
# the use of map !() method
# Getting the transformed elements.
a = zeros(3);
map !(x -> x * 3, a, [1, 2, 3]);
println(a)
b = ones(3);
map !(x -> x / 3, b, [3, 6, 9]);
println(b)
输出:
[3.0, 6.0, 9.0]
[1.0, 2.0, 3.0]