它工作时没有初始值:
reduce(+, [2 3 4])
尝试了多种提供初始值的方法--没有任何效果。
reduce(+, [2 3 4], 1)
reduce(+, 1, [2 3 4])也似乎减少只能使用两个参数运算符。使用接受当前值和累加器的自定义函数,应该使用什么函数来减少集合?下面的代码?
reduce((accumulator, value) -> push!(accumulator, value^2), [1, 2, 3], [])
# => [1, 4, 9]这个示例可以实现为map(x -> x^2, [1, 2, 3]),但我想知道如何用累加器实现它。
julia版本1.1.1
发布于 2019-06-11 21:08:33
init参数到reduce是一个关键字参数:
julia> reduce(+, [2 3 4], init = 1)
10
julia> reduce((accumulator, value) -> push!(accumulator, value^2), [1, 2, 3], init = [])
3-element Array{Any,1}:
1
4
9https://stackoverflow.com/questions/56551418
复制相似问题