Is inject the same thing as reduce in ruby?

白昼怎懂夜的黑 提交于 2019-11-27 11:23:47

问题


I saw that they were documented together here. Are they the same thing? Why does Ruby have so many aliases (such as map/collect for arrays)? Thanks a lot.


回答1:


Yes, and it's also called fold in 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 #length on an Array, you can. If you want to use #size, that's fine too!




回答2:


More recent versions of the documentation of Enumerable#reduce specify it explicitly:

The inject and reduce methods are aliases. There is no performance benefit to either.



来源:https://stackoverflow.com/questions/13813243/is-inject-the-same-thing-as-reduce-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!