Is inject the same thing as reduce in ruby?
Asked Answered
M

3

120

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.

Midshipmite answered 11/12, 2012 at 3:37 Comment(0)
B
167

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!

Bollay answered 11/12, 2012 at 3:44 Comment(9)
If you want to create your own aliases, you may be interested in alias_method.Informant
#length and #count are not equivalent in Ruby. #length and #size are.Pestalozzi
3.length gives NoMethodError; 3.size returns 4. Obviously 3 isn't an array, but beware of duck typing! (3_000_000_000_000_000.size returns 8 and 3_000_000_000_000_000_000_000_000_000_000_000_000_000_000.size returns 20; it seems to be the amount of memory it takes up.)Ridinger
hm, I think the current Ruby docs ruby-doc.org/core-2.2.3/Enumerable.html#method-i-reduce might be better if it just says the same as inject so that users don't have to read the two description and try to figure out if they are the same... doesn't this go with the DRY principle?Recording
@太極者無極而生, good point, I was confused by this just as you predicedSalyers
@Quolonel Questions, #length and #size are aliases, but #count (without any arguments) is, essentially, the same.Andrey
+1 on the documentation had both inject and reduce together with no explanation, would be clearer to say that they are equivalent. I googled because I was confused and ended up here.Turnedon
Err, why? This seems ridiculousAbsurdity
Is this equivalent to Haskell's foldl?Bermuda
L
9

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.

Louella answered 24/9, 2017 at 8:28 Comment(1)
i like to separate them based on how it's used, obviously just for semantics. if its a proc (&:+), reduce, if it's a block, injectAeroembolism
M
4

Are they the same thing?

Yes, aliases run the exact same code in the end.

Why does Ruby have so many aliases (such as map/collect for arrays)?

It boils down to the language's approach

Different languages have different approaches, I tried to visualize it here:

enter image description here

Ruby does it in favor of developer productivity. Basically, by having aliases you give programmers from different programming languages and human languages backgrounds to write code more intuitively.

However, they can also help your code's clarity because some things may have different semantic possibilities like the method midnight() can also be expressed as start_of_day or end_of_day. Those can be more clear depending on the context.

By the way, some programmers use inject and reduce to differentiate between different semantic situations too.

Myasthenia answered 15/2, 2021 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.