Inline for loops in Julia

I think you will be asked to use such cycles forin Julia:

b = [i+1 for i in [1,2,3]]

but I wonder why, besides a more compact syntax that leads to better readability. Are these inline loops more efficient for evaluating an expression? If so, why?

+4
source share
2 answers

These loops forare called arrays. They are a simple notation for a general pattern of programming the distribution of an array and then populating each element with a value based on some calculations.

, , , ( @inbounds). :

let tmp = [1,2,3], len = length(tmp)
    b = Vector{Int}(len)
    @inbounds for (n,i) in enumerate(tmp)
        b[n] = i+1
    end
    b
end

- , .

+5

@Dan Getz , , . b = [i+1.1 for i in tmp] 1 eltype b:

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

julia> b = [i+1.1 for i in [1,2,3]]
3-element Array{Float64,1}:
 2.1
 3.1
 4.1

for-loops b ( @Dan Getz: b = Vector{Int}(len)), , . , - , , , .


< > 1. , Juliav.5.5, , . , PR .

+4

Source: https://habr.com/ru/post/1687496/


All Articles