Rubynuby - confused by method name "inject"

A LeDonne wrote:

So, in math-speak, I think one could say:

inject applies a block pairwise as an n-ary operation over an
Enumerable, with an optional initial value.

This may or may not help anyone understand it. :slight_smile:

Shockingly [to myself], I actually understand that, but I think it
doesn’t cover all the possibile uses of inject, some of which are not
“operator in-betweenings”.

But thanks for sharing. Mentally relating product and summation to
inject helps me tolerate what I otherwise think is feasibly the most
unintuitively-named standard method I’ve ever encountered.

Pistos

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional.inject?

Don’t you confuse “stupid” with “the ones I am not used to”?

yieiayeld? How the heck is the meaningless nonsense spellllled?

You mean yield? Since when normal English word is meaningless nonsense?

Regards,
Rimantas

Jeff P. wrote:

I must say I’m surprised that my tired old thread is still kicking
around.

If I may summarize, the reason I was confused by Inject is that it is
useful but poorly named.

many thanks for all the replies,
jp

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.

On 3/1/07, Jenda K. [email protected] wrote:

The thread is still kicking exactly because names are important. And
when it comes to inventing stupid names Ruby is a professional. inject?
yieiayeld? How the heck is the meaningless nonsense spellllled? Not
speaking about the fact that restiricting (well, almost) the methods to
a single block/closure/function passed is silly, though not as silly as
doing so behind the scenes (not included in the argument list) and
calling it using a silly keyword.

I have to ask, from the apparent bitterness and outright hostility of
your frequent and recent posts – did a Ruby-programmed DARPA GRand
Challenge[1] contestant run over your puppy?

Jacob F.

[1] Defense Advanced Research Projects Agency - Content not found

On 3/1/07, Jenda K. [email protected] wrote:

The thread is still kicking exactly because names are important. And

it is not my style to feed trolls, but I just want to state:

you are polluting this ML
your posts are extremely annoying
I have no way to get rid of your posts without losing potential
information

If your email address is authentic I consider informing CPAN about
your behavior.

Have a nice day.

Robert

Since this old discussion is one of the top ranking pages when you
Google ‘ruby inject’ or ‘inject method’, it’s probably worth adding that
since version 1.8.7 Ruby has had the #reduce method, which is an alias
of #inject.

It seems a lot of people prefer #reduce, because it’s reducing the list
down to a single value. Personally I think it’s just as bad. Another
commenter said, the classic example of #inject is to sum the elements of
an array. So now we have a method called ‘reduce’ to add elements
together?! One might expect #reduce to work more like #drop or #take or
#select–to create a smaller (reduced) subset. To me it communicates
nothing of the combining nature of the method–unless maybe I think of
it in cooking terms, where one boils a sauce to condense it, but that’s
being generous I think.

It’s hard to come up with a good short name for such a versatile
function without implying an increase or decrease of successive results.
I think of the word ‘cumulative’ when I think of how #inject is using
the result of one iteration in the next one, and ‘cumulate’ is a verb,
but perhaps not a very common one, and it does kind of imply an increase
in successive results. ‘Accumulate’ is similar.

I wouldn’t mind Jeff’s suggestion of ‘combine’, except I think it could
very easily be confused with concatenation methods like #concat and #+,
which combines two arrays into one.

But then we have ‘fold’, which is the name Wikipedia went with in
categorising these types of functions. Why on earth couldn’t Ruby have
gone with this? Like ‘reduce’, it’s used by a stack of other languages,
but unlike ‘reduce’ it doesn’t imply a decrease in value. Check out the
top definitions of the word ‘fold’ from my dictionary:

fold
verb [ with obj. ]
1 bend (something flexible and relatively flat) over on itself so that
one part of it covers another: Sam folded up the map.
• (fold something in/into) mix an ingredient gently with (another
ingredient), especially by lifting a mixture with a spoon so as to
enclose it without stirring or beating: fold the egg whites into the
chocolate mixture.

So there’s that cooking metaphor again. But how elegant is that! No
metaphor is going to be a perfect fit for all that #inject is able to
do, but I reckon #fold would be a thousand times better than #inject and
#reduce.

I’m hoping they’ll add #fold as an alias method one day. But until then,
I guess I’ll stick with #inject.

I find #reduce much better than #inject, but #fold is my preference as
well. Consistent with other languages, and I’ve always found the
“folding
an ingredient into dough” metaphor apt.

But I can live with #reduce.

Fold is actually an older than functional programming concept for
recursive
computation which is just more syntax to the symbolic nature of these
languages. The inverse methods unfold/misfold would be the actual
removal,
deletions or cause for infection. Consider Stanford university’s gene
folding distributed computing research project:
http://folding.stanford.edu/

Many programmers come from different languages and have different
preferences to which /tokens/ yield the best value for their current
project. I enjoy using unix style commands like grep and so forth.
Someone
who has a background in either haskall, ocamal erlang or what not may
want
to use fold over reduce or use map or create their own datamapper and
declare their own name for it.

I use fold in my personal scripts. It’s really not that difficult to
create
your own. As far as having it in future release of the language the real
question is what would the inverse be misfold or unfold … or leave the
user to create their own.

Drop the cooking metaphor. It’s a biological concept. Consider reading
Alan
Kay for your meta and linguistic concepts.

Something about “map” + “fold” is pleasantly humorous.

On Fri, Jan 3, 2014 at 11:45 AM, Stu [email protected] wrote:

Drop the cooking metaphor. It’s a biological concept. Consider reading
Alan Kay for your meta and linguistic concepts.

This statement amuses me, because Alan Kay was specifically inspired by
biological concepts (cells) when he came up with his conception of OO
:slight_smile:

Stu wrote in post #1132133:

I use fold in my personal scripts. It’s really not that difficult to
create
your own. As far as having it in future release of the language the real
question is what would the inverse be misfold or unfold … or leave the
user to create their own.

Drop the cooking metaphor. It’s a biological concept.

Hey, if the protein folding metaphor works for you, great! Strike up
another reason why ‘fold’ is a better name than ‘inject’!

You lost me though when you started talking about ‘fold’ and ‘reduce’ as
two different things. No one here is arguing for a new method. The
thread is about the native #inject method in Ruby, and how that name
doesn’t help one intuitively understand or remember what it does.

On Fri, Jan 3, 2014 at 12:49 PM, Kal S. [email protected]
wrote:

The thread is about the native #inject method in Ruby, and how that
name doesn’t help one intuitively understand or remember what it does.

It sort of does, if you think of it as injecting the method or
block, between the values in the array. (And between the initial
value, if given, and the array.) So for instance:

arr = [1, 2, 3]
arr.inject(:slight_smile:

is like injecting “-” into the spaces between the values 1, 2, 3, so
you get 1, - 2, - 3. Speaking it with the usual pauses for the commas
also helps reinforce the order of operation (important with - or /
more so than + or *).

-Dave

Andrew V. wrote in post #1132140:

Something about “map” + “fold” is pleasantly humorous.

Now that you point that out, indeed it is! There you go, another fine
reason to add #fold.

(I’ve always preferred #map to #collect.)

What I’ve learned after many conversations about #inject and its aliases
is
that there is no one metaphor that works for this particular concept.
For
everyone who thinks “fold” makes perfect sense, there’s someone else who
thinks it’s nonsense and that “accumulate”, “aggregate”, “reduce”, or
“inject” make way more sense.

On Fri, Jan 3, 2014 at 1:54 PM, Dave A.

On Fri, Jan 3, 2014 at 2:06 PM, Avdi G. [email protected]
wrote:

What I’ve learned after many conversations about #inject and its aliases
is that there is no one metaphor that works for this particular concept.

Agreed. I didn’t grok why other languages called it “fold” until
someone told me the map metaphor. (Admittedly, Ruby is the only one
I’ve used a lot with such a concept.) The “inject into the spaces”
metaphor is one I came up with when trying to explain to a Ruby-newbie
what inject did. (Presumably others came up with it too; that may
even be how the name came about. Anybody know?) Reduce made sense
because I had already heard of map-reduce, but even then, as someone
else pointed out, “reduce” doesn’t necessarily mean “to one”.

If I were coming up with a name for it, for a new language, I think
“accumulate” would be my choice, by reference to the concept of an
accumulator. Even that, though, would be unclear to someone without a
solid CS/coding background already.

Maybe we should just make up a new word and confuse everybody equally.
Where’d I put that random D&D character name generator… :wink:

-Dave

Dave A. wrote in post #1132148:

The “inject into the spaces”
metaphor is one I came up with when trying to explain to a Ruby-newbie
what inject did. (Presumably others came up with it too;

Look no further than this very thread! See Jim W.'s first comment.

that may
even be how the name came about. Anybody know?)

It seems to take its name from Smalltalk’s inject:into: method. I’m not
familiar with Smalltalk, but here’s an example of that method taken from
Wikipedia:

#(1 3 5) inject: 10 into: [ :sum :element | sum + element ] “=> 19”

Based on how it reads ‘inject 10 into [block]’ (10 being the
first value for :sum), the idea seems to be of injecting the new result
back into the block upon each iteration, rather than injecting the block
between elements of the original object.

Reduce made sense
because I had already heard of map-reduce…

Yes, but wouldn’t map-fold be so much cooler? :slight_smile:

Wooosh! (The sound of Stu’s comment flying over my head.) I’ll have to
take your word for it mate! :slight_smile:

@Avdi

=) Yup. It’s a great concept. Smalltalk was built on another AI language
which uses cells which in turn branch in form of linked lists with a
atomic
dereference value and a pointer to the next cell in the tree. OO
messaging
is totally based on the concept where objects communicate with signals
and
messages over a network. The concept of a self described system wasn’t
exactly a new concept but definitely brought to a new level of control
moving into graphical systems.

@Kal
I didn’t refer to it being different. I mentioned that the alias to the
“sameness” of the functions will be applicable to current programmers
which
are writing code. Hence a SQL programmer might like /select/ a UNIX
programmer would use /grep/ a haskall programmer would feel comfortable
with /fold/ where a scheme programmer would use /reduce/. An erlang
programmer would embed their own MapReduce filter which would have a
slick
side effect providing the removal of the jvm virus inside an ad hoc
parser
on a zero width atomic contexts which would recursively reduce all ruby
code through an EvalApply self.reference reducer modulo whitespace into
a
try-retry cycled core routine emitting a prolog based truth maintenance
system implemented minikanren system built with nothing more than
regular
expressions!

It’s a programmable programming language! Know thy computational models
and
metaphors yet keep all aliases and subroutines intact and in core as to
respect all programmers with different backgrounds!

Jeff P. wrote in post #80239:

Can anybody explain to me how the Enumberable#inject method is
“injecting” something into something? I find it very difficult remember
method names when I don’t “get” them. So far in Ruby, “inject” takes
the cake for least understandable method name (with my own particular
convoluted gray matter).

Can somebody give some examples of its use and state in words how it is
“injecting” something into something?

thanks,
jp

P.S.
If anybody wants to take a stab at “collect”, that would be welcome
also.

My brain works exactly the same way. In fact thats why i wound up here
reading your question! Thanks for asking it.

Michael

@Kal maybe this will help:

Here is the concept in done fizzbuzz style list :
The problem: Write a function foo that takes a number n and returns a
function that takes a number i, and returns n incremented by i.

http://www.paulgraham.com/accgen.html

which is an extension to this inglorious rant on managerial driven
development (MDD)
http://www.paulgraham.com/icad.html

~Stu