简体   繁体   English

Julia,for循环迭代器增加到零

[英]Julia, for loop iterator increase to zero

I have a nice function that sums a group of random numbers generated with this function.我有一个不错的 function,它总结了用这个 function 生成的一组随机数。

sum(round(Int8, floor(
        rand(TruncatedNormal(var1, var2, var3, var4))))
                     for _ in 1:var5)

Most of the time it works fine, var5, the iterator for the for loop is calculated earlier in a function, and can legitimately be a zero.大多数情况下它工作正常,var5, for循环的迭代器是在 function 中较早计算的,并且可以合法地为零。 Therefore iterating by increasing from 1 to 0 doesn't work and throws an error.因此,从 1 增加到 0 的迭代不起作用并引发错误。 Is there a clean "check" in Julia that will simply produce a result of 0 for the whole function, or do I need to go back and enclose this function in an if statement to check the value of var5 prior to running it, and if var5<=0 , produce an alternate result (0)? Is there a clean "check" in Julia that will simply produce a result of 0 for the whole function, or do I need to go back and enclose this function in an if statement to check the value of var5 prior to running it, and if var5<=0 ,产生替代结果(0)? Thx.谢谢。 J Ĵ

When you do sum(rand(Int8) for _ in 1:0) you get the error:当您sum(rand(Int8) for _ in 1:0)时,您会收到错误消息:

julia> sum(rand(Int8) for x in 1:0)
ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
 [1] _empty_reduce_error() at ./reduce.jl:299
 [2] mapreduce_empty(::Function, ::Base.BottomRF{typeof(Base.add_sum)}, ::Type{T} where T) at ./reduce.jl:342
 [3] reduce_empty(::Base.MappingRF{var"#9#10",Base.BottomRF{typeof(Base.add_sum)}}, ::Type{Int64}) at ./reduce.jl:329
 [4] reduce_empty_iter at ./reduce.jl:355 [inlined]
 [5] reduce_empty_iter at ./reduce.jl:354 [inlined]
 [6] foldl_impl at ./reduce.jl:49 [inlined]
 [7] mapfoldl_impl at ./reduce.jl:44 [inlined]
 [8] #mapfoldl#204 at ./reduce.jl:160 [inlined]
 [9] mapfoldl at ./reduce.jl:160 [inlined]
 [10] #mapreduce#208 at ./reduce.jl:287 [inlined]
 [11] mapreduce at ./reduce.jl:287 [inlined]
 [12] sum at ./reduce.jl:494 [inlined]
 [13] sum(::Base.Generator{UnitRange{Int64},var"#9#10"}) at ./reduce.jl:511
 [14] top-level scope at REPL[6]:1

One very simple workaround is to collect the random numbers into an array first — or simply use a comprehension instead of a generator:一个非常简单的解决方法是首先将随机数收集到一个数组中——或者简单地使用推导式而不是生成器:

julia> sum([rand(Int8) for x in 1:0])
0

You could also use the ternary expression as linked above to simply avoid the problem altogether:您还可以使用上面链接的三元表达式来完全避免该问题:

julia> var5 = 0
0

julia> var5 < 1 ? 0 : sum(rand(Int8) for x in 1:var5)
0

julia> var5 = 2
2

julia> var5 < 1 ? 0 : sum(rand(Int8) for x in 1:var5)
49

For cases where you need to catch the case of summing over an empty collection, I prefer an explicit reduce with an init value:对于需要捕获对空集合求和的情况,我更喜欢带有init值的显式reduce

julia> reduce(+, (rand(Int8) for x in 1:0), init=Int8(0))
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM