【发布时间】:2015-01-14 23:23:25
【问题描述】:
在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 矩阵,这可能不是在编程上下文中想象数组的最佳方式?
我意识到您可能会写整本关于什么是数据结构的书籍,当然我也可以通过 Google 搜索,但我发现对某个主题有深刻理解的人能够更简洁地解释它(并且也许简化)的方式那么让我们说维基百科文章可以,这就是我问你们(和女孩)的原因。
【问题讨论】: