def initialize(apps, catch=404)
@apps = []; @has_app = {}
apps.each { |app| add app }
@catch = {}
[*catch].each { |status| @catch[status] = true }
end
Run Code Online (Sandbox Code Playgroud)
在Rack :: Cascade的这个方法splat(*)中,[*catch]代码服务的目的是什么?
我认为在方法参数中使用了一个splat来指示何时你将有一个未指定数量的参数.
splat在这里有不同的含义吗?
fruit = ["apple","red","banana","yellow"]
=> ["apple", "red", "banana", "yellow"]
Hash[*fruit]
=> {"apple"=>"red", "banana"=>"yellow"}
Run Code Online (Sandbox Code Playgroud)
为什么splat导致数组被如此整齐地解析为Hash?
或者,更确切地说,哈希如何"知道""苹果"是关键而"红色"是它的对应值?
是因为它们在水果阵列中处于连续位置吗?
在这里使用splat是否重要?哈希不能直接从arry定义自己吗?
运行以下代码,
a = [1, 2, 3, 4, 5]
head, *tail = a
p head
p tail
Run Code Online (Sandbox Code Playgroud)
你会得到结果
1
[2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
谁能帮我解释一下这个说法head,*tail = a,谢谢!
我知道当我们不知道将传递的参数数量时,会使用splat参数.我想知道我是否应该一直使用splat.每当我传递参数时,使用splat参数是否有任何风险?
b下面的所有值都让我用*args语法调用方法.
def some_method(a)
puts a
end
b = 1
some_method(*b) # => 1
b = false
some_method(*b) # => false
b = "whatever"
some_method(*b) # => "whatever"
Run Code Online (Sandbox Code Playgroud)
有了nil,我期望得到nil,而不是参数错误:
b = nil
some_method(*b) # => ArgumentError: wrong number of arguments (0 for 1)
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
我正在尝试编写一个函数来调用几个接受命名参数的函数.我希望A函数能够在args中将参数命名为splatted,并将匹配的参数传递给它调用的函数.
function A(x, y; args...)
B(x; args...)
C(y; args...)
end
function B(x; a=0)
println(x,a)
end
function C(y; a=0, b=0)
println(y,a,b)
end
funcA(1, 2) # Works
funcA(1, 2, a=1) # Works
funcA(1, 2, a=1, b=1) # Error: unrecognized keyword argument "b"
Run Code Online (Sandbox Code Playgroud)
让它工作的首选方法是什么?将"args ..."添加到B的参数列表中可以修复错误,但我不确定这是否是一个好主意(例如是否有任何性能损失).
我想传递一个可选参数(任何数据类型,甚至是哈希),然后是一个方法的关键字参数列表(可以为空).
这就是我得到的:
def my_method(subject = nil, **options)
[subject, options]
end
Run Code Online (Sandbox Code Playgroud)
在以下情况下,方法输出符合预期:
没有
my_method()
# => [nil, {}]
Run Code Online (Sandbox Code Playgroud)课程
my_method('foo')
# => ["foo", {}]
Run Code Online (Sandbox Code Playgroud)一个有选项的文字主题
my_method('foo', bar: 'bar')
# => ["foo", {:bar=>"bar"}]
Run Code Online (Sandbox Code Playgroud)a Hash作为选项的主题
my_method({foo: 'foo'}, bar: 'bar')
# => [{:foo=>"foo"}, {:bar=>"bar"}]
Run Code Online (Sandbox Code Playgroud)没有主题,只有选项
my_method(bar: 'bar')
# => [nil, {:bar=>"bar"}]
Run Code Online (Sandbox Code Playgroud)当传递Hash作为主题而没有选项时,期望的结果是:
my_method({foo: 'foo'})
# => [{:foo=>"foo"}, {}]
Run Code Online (Sandbox Code Playgroud)
但我得到以下内容; 我没有得到正确的主题:
my_method({foo: 'foo'})
# => [nil, {:foo=>"foo"}]
Run Code Online (Sandbox Code Playgroud)
是my_method(foo: 'foo')相当于my_method({foo: 'foo'})?关于如何获得理想结果的任何想法?
我有一堆这样的结构,其中成员数量不断增加,但成员命名却保持一致:
struct one { int a; };
struct two { int a; int b; };
struct three { int a; int b; int c; };
Run Code Online (Sandbox Code Playgroud)
我也有一个模板化函数,我希望接受以下这些结构的成员之一:
template <typename T, typename ... ARGS> // T will be one, two, or three
void func(ARGS... args); // This should take 1, 2, or 3, int arguments respectively
Run Code Online (Sandbox Code Playgroud)
我希望能够这样称呼:
two foo;
func<two>(splatter(foo));
Run Code Online (Sandbox Code Playgroud)
哪里splatter会以某种方式分裂,foo以便解决func<two>(foo.a, foo.b)。
显然,我可以只扩展此内联,而无需splatter,但是我调用的代码func本身很容易被模板化。我已经尝试过使用,initializer_list但是我不知道如何仅基于模板类型来构建一个。
不幸的是,我的编译器也不支持constexpr if对splat的调用func或构建initializer_list …
当使用splat运算符通过引用将参数传递给块时,似乎会复制参数.
我有这个:
def method
a = [1,2,3]
yield(*a)
p a
end
method {|x,y,z| z = 0}
#=> this puts and returns [1, 2, 3] (didn't modified the third argument)
Run Code Online (Sandbox Code Playgroud)
我如何通过引用传递这些参数?如果我直接传递数组似乎可行,但splat运算符在这里更实用,直观和可维护.
在许多过程语言(例如 python)中,我可以“解包”一个列表并将其用作函数的参数。例如...
def print_sum(a, b, c):
sum = a + b + c
print("The sum is %d" % sum)
print_sum(*[5, 2, 1])
Run Code Online (Sandbox Code Playgroud)
此代码将打印:“ The sum is 8”
有没有办法在 Prolog 中复制这种参数解包行为?
例如,我想在将列表变量传递给call之前解压缩它。
我可以写一个这样的谓词吗?
assert_true(Predicate, with_args([Input])) :-
call(Predicate, Input).
% Where `Input` is somehow unpacked before being passed into `call`.
Run Code Online (Sandbox Code Playgroud)
...然后我可以查询
?- assert_true(reverse, with_args([ [1, 2, 3], [3, 2, 1] ])).
% Should be true, currently fails.
?- assert_true(succ, with_args([ …Run Code Online (Sandbox Code Playgroud)