Robert Klemme wrote:
> "Alexander Kellett" <ruby-lists / lypanov.net> schrieb im Newsbeitrag
> news:9b411145f76db3c8a017a89ebb9d5611 / lypanov.net...
> > the main problem i've had with inject is the naming.
> > to me it says 'destructive'. [...]
> #accum is the only name that fits slightly better than #inject
although I'm
> sure one can come up with misinterpretations of that name also.
Anyway:
> #inject is quite complex so it takes some time to get used to the
behavior
> regardless of how it's named.
>
> <rant>
> But: the search for a better name is wasted time IMHO.  Things are
like they
> are and changes to base classes generally do far more harm than good
(if
> something is seriously flawed that's another story of course).
> </rant>

I respectfully disagree. I was SURE from the name that it must be some
function to modify the original array, inserting new entries. It took
me a long time to understand what it was for.

Obviously we can't ditch the old name (for backwards-compatibility
reasons) but a better alias that is promoted to new users would be a
good idea, IMHO.

How about #each_with_state for a name? #accumulate (or #accum) is not a
bad name, but also implies to me that the result will be the
sum/product/concatenation of values. How would #accum fit for the ri
example of using #inject to find the longest item in a list, for
example? Some form of accumulation is one of the more common uses of
#inject (I gather) but by no means the only use.


Aside - whatever the name, what an excellent, versatile function! For
example, the linked-to geometric series sum at
http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt can be
written in a more terse and (IMO) more ruby-esque way as simply:

def geometric_series_sum( x, n )
	(0..n).inject(0){ |sum,i| sum+x**i }
end

--
(-, /\ \/ / /\/