ruby 注入与减少红宝石相同吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13813243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is inject the same thing as reduce in ruby?
提问by Hymany
回答by Zach
Yes, and it's also called foldin many other programming languages and in Mathematics. Ruby aliases a lot in order to be intuitive to programmers with different backgrounds. If you want to use #lengthon an Array, you can. If you want to use #size, that's fine too!
是的,它fold在许多其他编程语言和数学中也被调用。Ruby 别名很多,以便对不同背景的程序员直观。如果你想在#length上使用Array,你可以。如果你想使用#size,那也没关系!
回答by axiac
More recent versions of the documentation of Enumerable#reducespecify it explicitly:
文档的更新版本Enumerable#reduce明确指定它:
The
injectandreducemethods are aliases. There is no performance benefit to either.
该
inject和reduce方法是别名。两者都没有性能优势。

