Apply a callback to an array: Difference between revisions

Content added Content deleted
No edit summary
(Updated Julia)
Line 970: Line 970:
=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>
<lang julia>
a = [1,2,3,4,5]
numbers = [1, 3, 5, 7]
julia> map(x -> x*2,a)
square1 = [square(n) for n in numbers] # list comprehension
5-element Int32 Array:
2
squares2a = map(square, numbers) # functional form
4
6
squares2b = map(x -> x*x, numbers) # functional form with `lambda`
8

10
#There is also extended block form for the map function
</lang>
squares2c = map(numbers) do x
.* ./ .\ .^
sum = 0
The element-wise binary addition, subtraction, multiplication, left division, right division, and exponentiation operators
for i = 1:x
<lang julia>
sum += x^2 #trivial, but you get the point
julia> a .* 2
end
5-element Int32 Array:
return sum
2
end
4

6
squares3 = [n * n for n in numbers] # no need for a function,
8

10
squares4 = numbers .* numbers #element-wise operation
</lang>

squares4a = numbers .^ 2 #includes .+, .-, ./, comparison, and bitwise operations as well</lang>


=={{header|Lang5}}==
=={{header|Lang5}}==