Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 林克地图!或收集!_Ruby_Linq_Map_Collect - Fatal编程技术网

Ruby 林克地图!或收集!

Ruby 林克地图!或收集!,ruby,linq,map,collect,Ruby,Linq,Map,Collect,Linq与Ruby中的方法等价于什么 a = [ "a", "b", "c", "d" ] a.collect! {|x| x + "!" } a #=> [ "a!", "b!", "c!", "d!" ] 我可以通过使用foreach迭代集合来实现这一点,但我想知道是否有更优雅的Linq解决方案。Map=Select var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!

Linq与Ruby中的方法等价于什么

   a = [ "a", "b", "c", "d" ]
   a.collect! {|x| x + "!" }
   a             #=>  [ "a!", "b!", "c!", "d!" ]
我可以通过使用foreach迭代集合来实现这一点,但我想知道是否有更优雅的Linq解决方案。

Map=Select

var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!");

高阶函数
map
最能体现在
System.Linq
中的扩展方法中

如果您感到好奇,其他高阶函数如下所示:

reduce->


filter->

Linq采用函数式方法,因此通常不会像上面的示例那样进行就地修改。然而,这更符合ruby中map和collect的预期用途(没有!)