# Inject is pathetic?

**URL:** <https://www.ruby-forum.com/t/inject-is-pathetic/222892>\
**Category:** Ruby\
**Created:** [September 13, 2012, 8:31pm UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892 "2012-09-13T20:31:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 13, 2012, 8:31pm UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/1 "2012-09-13T20:31:45Z")

</div>

On Wed, Sep 12, 2012 at 11:46 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just forget inject() even exists. It’s really quite a pathetic method.

What do you mean by that?

-Dave

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 14, 2012, 5:00am UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/2 "2012-09-14T05:00:42Z")

</div>

Dave A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, Sep 12, 2012 at 11:46 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Just forget inject() even exists. It’s really quite a pathetic method.
> 
> What do you mean by that?

He probably just doesn’t grasp how fundamental it is. After all, no one  
would call “join” pathetic, but it is simply a special case of inject.  
Or, to put it a better way, inject abstracts intuitively obvious notions  
such as join, sum, and product. And in Ruby 1.9.3 it gets even cooler,  
since you can just say:

[1,2,3].inject(:+)

Not everyone knows LISP or has read The Structure and Interpretation of  
Computer Programs. Those who have, know. m.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 14, 2012, 5:25am UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/3 "2012-09-14T05:25:14Z")

</div>

Agree. #inject is essential for Array.

On Sep 14, 2012, at 11:00 AM, Matt N. wrote:

> Or, to put it a better way, inject abstracts intuitively obvious notions  
> A fool + a tool + an autorelease pool = cool!  
> AppleScript: the Definitive Guide - Second Edition!  
> [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/default.html#applescriptthings)

Best regards,  
Zhi-Qiang L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 14, 2012, 12:20pm UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/4 "2012-09-14T12:20:44Z")

</div>

On Fri, Sep 14, 2012 at 5:46 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> strings.inject(“”) { |joined, current| joined + current }
> 
> is slow and wasteful because it created all the intermediate forms: “”, “a”,  
> “ab”, “abc”, “abcd”, “abcde”

but josh, isn’t that because of String#+ ?

there is always String#\<\< or String#concat.

nonetheless, am also a fan of inject syntax, and hope to see someday,  
a more super fast version of inject. The inject syntax is stupidly  
generic/flexible, that is why many luv it, including me 😉

btw, i usually use each.with\_object instead of each\_with\_object. And i  
alias with\_object to using\_object. but hey, that is just me. but  
regardless, we all are fan of ruby, right? to each, his each, i guess  
😉

kind regards -botp

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 14, 2012, 1:07pm UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/5 "2012-09-14T13:07:29Z")

</div>

On Fri, Sep 14, 2012 at 5:19 AM, botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Fri, Sep 14, 2012 at 5:46 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > strings.inject(“”) { |joined, current| joined + current }
> > 
> > is slow and wasteful because it created all the intermediate forms: “”,  
> > “a”,  
> > “ab”, “abc”, “abcd”, “abcde”
> 
> but josh, isn’t that because of String#+ ?

Yes

> there is always String#\<\< or String#concat.

You’re right, since String#\<\< returns self, you can do it efficiently  
with  
inject:

[\*‘a’..‘e’].inject(‘’, :\<\<) # =\> “abcde”

Instead of

[\*‘a’..‘e’].inject(‘’, :+) # =\> “abcde”

It deviates from the more functional ideas of immutable data, which is  
where inject makes the most sense, but in Ruby this case will work  
sufficiently.

When return values don’t match up nicely like this, it gets cumbersome  
(you have to go to multiple lines or use semicolons):

%w[a b a c].inject(Hash.new 0) { |sums, char| sums[char] += 1; sums }  
%w[a b a c].each\_with\_object(Hash.new 0) { |char, sums| sums[char] += 1  
}

> btw, i usually use each.with\_object instead of each\_with\_object. And i  
> alias with\_object to using\_object. but hey, that is just me. but  
> regardless, we all are fan of ruby, right? to each, his each, i guess  
> 😉

I only do .with\_object when I’m working with an arbitrary enumerator,  
because I expect it has overhead associated with its laziness. I don’t  
know  
how it is implemented (it takes a lot of effort for me to figure out  
what C  
Ruby is doing), but I think of it basically like this:

List = Struct.new :element, :child do  
def each\_with\_object(object, &block)  
block[element, object]  
child && child.each\_with\_object(object, &block)  
object  
end

def each(&block)  
return LazyIteration.new self, :each unless block  
block[element]  
child && child.each(&block)  
end

LazyIteration = Struct.new :target, :message do  
def with\_object(object, &block)  
target.send(message) { |element| block[element, object] }  
object  
end  
end  
end

list = List.new(‘a’, List.new(‘b’, List.new(‘c’, nil)))

list.each\_with\_object(‘’) { |element, aggregate| aggregate \<\< element }

# 

=\> “abc”  
list.each.with\_object(‘’) { |element, aggregate| aggregate \<\< element }

# 

=\> “abc”

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 14, 2012, 10:40pm UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/6 "2012-09-14T22:40:53Z")

</div>

Josh C. wrote in post #1075977:

> But in Ruby
> 
> strings.inject(“”) { |joined, current| joined + current }
> 
> is slow and wasteful because it created all the intermediate forms: “”,  
> “a”, “ab”, “abc”, “abcd”, “abcde”  
> Whereas each\_with\_object only creates: “abcde”

Correct, in ruby, because ruby isn’t optimised for funtional use.  
Another example is that there is no tail-recursion unless you build ruby  
with a special compile-time option.

But in other languages, what you’ve written is actually efficient. For  
example, Erlang has a specific optimisation for binaries which are  
appended to, where no reference remains to the previous incarnation of  
the binary.

> Your example is compelling to you, because you’re enamored with the  
> implementation

I’d say the opposite; in ruby the implementation is not so good, but  
it’s the concept, the abstraction, which is attractive.

foo.each =\> do something for each element of an array  
foo.inject =\> calculate a summary value across all elements of an array

In fully functional languages, foo.each makes no sense because “do  
something” makes no sense - functions don’t have side-effects. (Erlang  
is _not_ such a language, by the way, but is part way because it doesn’t  
have mutable variables).

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 16, 2012, 6:42am UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/7 "2012-09-16T06:42:15Z")

</div>

> Correct, in ruby, because ruby isn’t optimised for funtional use.

Interesting. Will it be?

Best regards,  
Zhi-Qiang L.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 16, 2012, 10:00am UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/8 "2012-09-16T10:00:51Z")

</div>

On 16 September 2012 14:41, Zhi-Qiang L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Correct, in ruby, because ruby isn’t optimised for funtional use.
> 
> Interesting. Will it be?

I strongly doubt it, there’s too much OO in the core for the language  
to ever lean very far in a stateless (functional) direction.

–  
Matthew K., B.Sc (CompSci) (Hons)  
[http://matthew.kerwin.net.au/](http://matthew.kerwin.net.au/)  
ABN: 59-013-727-651

“You’ll never find a programming language that frees  
you from the burden of clarifying your ideas.” - xkcd

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 16, 2012, 7:19pm UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/9 "2012-09-16T19:19:18Z")

</div>

On Sun, Sep 16, 2012 at 1:00 AM, Matthew K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> I strongly doubt it, there’s too much OO in the core for the language  
> to ever lean very far in a stateless (functional) direction.

FP and OO are not orthogonal. Perhaps the word you were looking for was  
“mutation”

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [September 14, 2012, 11:46am UTC](https://www.ruby-forum.com/t/inject-is-pathetic/222892/10 "2012-09-14T11:46:49Z")

</div>

On Thu, Sep 13, 2012 at 10:00 PM, Matt N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Or, to put it a better way, inject abstracts intuitively obvious notions  
> such as join, sum, and product. And in Ruby 1.9.3 it gets even cooler,  
> since you can just say:
> 
> [1,2,3].inject(:+)

I disagree. Nearly everything you want to use inject for,  
each\_with\_object  
is better. Array#join, for example is better implemented using  
each\_with\_object than inject

strings.each\_with\_object(“”) { |current, joined| joined \<\< current }

Perhaps in a language like Haskell where strings are just lists of  
characters, then you could join them in a single pass, if you started at  
the end and worked backwards. Or perhaps the thunk could be smart enough  
to  
compose or allow access to it without creating all the intermediate  
forms.  
But in Ruby

strings.inject(“”) { |joined, current| joined + current }

is slow and wasteful because it created all the intermediate forms: “”,  
“a”, “ab”, “abc”, “abcd”, “abcde”  
Whereas each\_with\_object only creates: “abcde”

Your example is compelling to you, because you’re enamored with the  
implementation, but `[1,2,3].inject(:+)` is ugly, confusing, and buggy.  
If  
we could say something like `[1,2,3].sum_from 0`, this would be clearer  
because we don’t care about the implementation. When we see  
`.inject(:+)`  
we have to mentally translate it “oh, we’re getting the sum”. That cost  
adds up, things like this should be declarative. It’s buggy because if  
your  
array is empty, you get `[].inject(:+) # => nil`, but the null object  
for a  
sum is 0, so you have to remember to pass the initial argument  
`[].inject(0, :+) # => 0` (that param should probably be required). It’s  
further ugly because it breaks from Ruby conventions. When you have a  
method that takes a block, everywhere else you would have to say  
`[].inject(0, &:+)`, but here, translating the symbol into a block is  
moved  
into the method, causing you to think you have a bug until you go read  
the  
docs enough times to remember this one-off case.

And in the few cases where inject does make more sense than  
each\_with\_object, it may be clearer to just iterate over the array with  
#each, and update a local variable.

> Not everyone knows LISP or has read The Structure and Interpretation of  
> Computer Programs. Those who have, know. m.

I generally like Lisp, but the elitism around it is just obnoxious, as  
in  
this quote (at least for Common Lisp and Scheme, Clojure seems more down  
to  
earth).
