小编jsh*_*ort的帖子

Ruby 2.0.0 Array #bsearch行为

我注意到,从Ruby 2.0.0开始,数组类有一个bsearch我正在测试的方法,而且我没有得到我期望的行为.为什么它返回2和5的值,但是nil-1,1和4?

arr_in = [-1, 1, 2, 4, 5]

arr_in.bsearch { |x| x == 3 }   #=> nil
arr_in.bsearch { |x| x == -1 }  #=> nil
arr_in.bsearch { |x| x == 1 }   #=> nil
arr_in.bsearch { |x| x == 2 }   #=> 2
arr_in.bsearch { |x| x == 4 }   #=> nil
arr_in.bsearch { |x| x == 5 }   #=> 5
Run Code Online (Sandbox Code Playgroud)

ruby arrays binary-search bsearch

19
推荐指数
2
解决办法
3882
查看次数

Golang返回函数的值作为另一个的输入参数

如果我有

func returnIntAndString() (i int, s string) {...}
Run Code Online (Sandbox Code Playgroud)

我有:

func doSomething(i int, s string) {...}
Run Code Online (Sandbox Code Playgroud)

然后我可以成功完成以下操作:

doSomething(returnIntAndString())
Run Code Online (Sandbox Code Playgroud)

但是,假设我想在doSomething中添加另一个参数:

func doSomething(msg string, i int, s string) {...}
Run Code Online (Sandbox Code Playgroud)

如果我将其称为:

doSomething("message", returnIntAndString())
Run Code Online (Sandbox Code Playgroud)

附:

main.go:45: multiple-value returnIntAndString() in single-value context
main.go:45: not enough arguments in call to doSomething()
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,或者我应该放弃并将返回值分配returnIntAndString给一些引用并传递msg和这些值如doSomething(msg, code, str)

parameter-passing go

9
推荐指数
1
解决办法
4688
查看次数

ruby中优先级和/或方法参数的优先级

这是两个测试:

if [1,2,3,4].include? 2 && nil.nil?
  puts :hello
end
#=>
Run Code Online (Sandbox Code Playgroud)

if [1,2,3,4].include?(2) && nil.nil?
  puts :hello
end
#=> hello
Run Code Online (Sandbox Code Playgroud)

上面告诉我,它&&具有比方法参数更高的优先级,所以它在逻辑上2 && nil.nil?是正确的,并将其作为参数传递给包含?.

但是,有这个测试:

if [1,2,3,4].include? 2 and nil.nil?
  puts :hello
end
#=> hello
Run Code Online (Sandbox Code Playgroud)

所以这告诉我方法参数和' and'具有相同的优先级(或方法args高于' and'),因为它传递2包含?在它处理'和'之前.

注意:我理解&&并且and具有不同的优先级.问题不在于此,而是与ruby方法的参数相关and或相对or.

我找不到证实这一点的文件.例如,这根本没有提到方法参数:http://phrogz.net/programmingruby/language.html#table_18.4http://romhack.wikia.com/wiki/Ruby_operators.

有谁能解释这种行为?也就是说,ruby如何知道将值作为参数传递给方法与流程操作符?

ruby operator-precedence

7
推荐指数
1
解决办法
393
查看次数

如何有条件地为引用分配类型

我今天下午一直在玩Rust,并决定编写一个简单的哈希工具,可以做所有主要的消化算法.

我正在尝试做这样的事情(意图应该是显而易见的):

let mut hasher;
match alg {
    "md5" => { hasher = Md5::new() }
    "sha1" => { hasher = Sha1::new() }
    _ => { println!("Algorithm not implemented");
           process::exit(1); }
}
hash_file(&file_name, &mut hasher).unwrap();
Run Code Online (Sandbox Code Playgroud)

在编译上面时,由于第一次匹配,它假定hasher属于类型Md5并且在"sha1"匹配分支中失败,它尝试分配Sha1类型.我打算在这个匹配语句中使用的所有类型都是特征的实现者,Digest所以我觉得应该有办法做到这一点.

我试过了:

let mut hasher: Digest;
Run Code Online (Sandbox Code Playgroud)

但这也不起作用.

rust

2
推荐指数
2
解决办法
161
查看次数

左/右递归和 Bison 解析堆栈行为

因此,在我< 24 小时的野牛/ flex 调查中,我看到很多文档表明左递归优于右递归。有些地方甚至提到,对于左递归,您需要在 Bison 解析器堆栈上保持恒定空间,而右递归则需要 N 阶空间。但是,我找不到任何可以明确解释正在发生的事情的来源。

作为一个例子(只加减的解析器):

扫描器:

%%
[0-9]+ {return NUMBER;}
%%
Run Code Online (Sandbox Code Playgroud)

解析器:

%%
/* Left */
expression:
    NUMBER
  | expression '+' NUMBER { $$ = $1 + $3; }
  | expression '-' NUMBER { $$ = $1 - $3; }
  ;
/* Right */
expression:
    NUMBER
  | NUMBER '+' expression { $$ = $1 + $3; }
  | NUMBER '-' expression { $$ = $1 - $3; }
  ;
%%
Run Code Online (Sandbox Code Playgroud)

对于 1+5-2 的示例,似乎在左递归中,解析器从词法分析器接收 '1' 并看到 …

recursion yacc lex bison flex-lexer

1
推荐指数
1
解决办法
1398
查看次数