Julia 朱莉娅:函数数组
我试图在Julia中填充一个函数数组。一个最小的例子是Julia 朱莉娅:函数数组,julia,Julia,我试图在Julia中填充一个函数数组。一个最小的例子是 function nice() i=1 while true f() = i produce(f) i+=1 end end ye = Task(() -> nice()) funcs = Function[] for i in [1:2] push!(funcs,consume(ye)) println("Why does this not stay the same???") p
function nice()
i=1
while true
f() = i
produce(f)
i+=1
end
end
ye = Task(() -> nice())
funcs = Function[]
for i in [1:2]
push!(funcs,consume(ye))
println("Why does this not stay the same???")
println(funcs[1]())
end
问题是funcs[1]从返回1的函数更改为返回2的函数!请帮帮我 这就解决了问题。我需要一个let命令和f=()->j语句
function nice()
i=1
while true
let j=i
f=()->j
produce(f)
end
i+=1
end
end
ye = Task(() -> nice())
funcs = Function[]
for k in [1:2]
push!(funcs,consume(ye))
end
println(funcs[1]())
println(funcs[2]())