许多嵌套for循环的替代品?

pur*_*ift 1 julia

出于科学目的,我必须一遍又一遍地评估8个参数的函数,这些参数在某些范围内取值.

是否有比8嵌套for循环的简单代码更聪明(更好的表现)?

谢谢

DNF*_*DNF 6

你可以使用Iterators.product,它结合了多个迭代器,如下所示:

julia> for (i, j, k) in Iterators.product(1:2, 5:6, -2:-1)
       println("hi, $i, $j and $(k)!")
       end
hi, 1, 5 and -2!
hi, 2, 5 and -2!
hi, 1, 6 and -2!
hi, 2, 6 and -2!
hi, 1, 5 and -1!
hi, 2, 5 and -1!
hi, 1, 6 and -1!
hi, 2, 6 and -1!
Run Code Online (Sandbox Code Playgroud)

您也可以将其作为元组返回,将其编入索引,如下所示:

julia> for K in Iterators.product(1:2, 5:6, -2:-1)
       println("hi, $(K[1]), $(K[2]) and $(K[3])!")
       end
hi, 1, 5 and -2!
hi, 2, 5 and -2!
hi, 1, 6 and -2!
hi, 2, 6 and -2!
hi, 1, 5 and -1!
hi, 2, 5 and -1!
hi, 1, 6 and -1!
hi, 2, 6 and -1!
Run Code Online (Sandbox Code Playgroud)