Is inject the same thing as reduce in ruby?

前端 未结 2 1978
北海茫月
北海茫月 2020-12-13 16:29

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.

相关标签:
2条回答
  • 2020-12-13 17:13

    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!

    0 讨论(0)
  • 2020-12-13 17:23

    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.

    0 讨论(0)
提交回复
热议问题