在http://learnxinyminutes.com/docs/julia/上阅读有关 Julia 的信息时,我遇到了这个问题:
# You can define functions that take a variable number of
# positional arguments
function varargs(args...)
return args
# use the keyword return to return anywhere in the function
end
# => varargs (generic function with 1 method)
varargs(1,2,3) # => (1,2,3)
# The ... is called a splat.
# We just used it in a function definition.
# It can also be used in a fuction call,
# where it will splat an Array or Tuple's contents into the argument list.
Set([1,2,3]) # => Set{Array{Int64,1}}([1,2,3]) # produces a Set of Arrays
Set([1,2,3]...) # => Set{Int64}(1,2,3) # this is equivalent to Set(1,2,3)
x = (1,2,3) # => (1,2,3)
Set(x) # => Set{(Int64,Int64,Int64)}((1,2,3)) # a Set of Tuples
Set(x...) # => Set{Int64}(2,3,1)
我敢肯定这是一个非常好的解释,但是我无法掌握主要思想/好处。
据我目前了解:
- 在函数定义中使用 splat 允许我们指定我们不知道函数将给出多少个输入参数,可能是 1,可能是 1000。不要真正看到这样做的好处,但至少我理解 (我希望)这个概念。
- 使用 splat 作为函数的输入参数会......究竟是什么?我为什么要使用它?如果我必须将数组的内容输入到参数列表中,我将使用以下语法:some_array(:,:)(对于 3D 数组,我将使用 some_array(:,:,:) 等)。
我认为我不明白这一点的部分原因是我在努力定义元组和数组,是 Julia 中的元组和数组数据类型(如 Int64 是一种数据类型)吗?或者它们是数据结构,什么是数据结构?当我听到数组时,我通常会想到 2D 矩阵,这可能不是在编程上下文中想象数组的最佳方式?
我意识到你可能会写整本关于什么是数据结构的书,我当然可以用谷歌搜索它,但是我发现对一个主题有深刻理解的人能够用更简洁(也许是简化)来解释它那么让我们说维基百科文章可以,这就是我问你们(和女孩)的原因。