From: Meinrad Recheis Date: 2008-06-11T07:09:42+09:00 Subject: [ruby-core:17202] Re: Enumerable::Enumerator#with_memo ------=_Part_35391_31001879.1213135796881 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, Jun 10, 2008 at 11:42 PM, Martin DeMello wrote: > On Tue, Jun 10, 2008 at 10:44 AM, David Flanagan > wrote: > > Martin DeMello wrote: > >> > >>>> ary.each.accumulating(0) {|i, a| a += i} > >> > >> It should initialize a to 0, mutate it with each pass due to the > >> side effect of a += i, and return the final value. I'm not relying on > >> the return value at all. > > > > Numeric values are not mutable. a += 1 assigns a new object to a. > > Doh! That's a limitation of the method, then, since what we *really* > want to do is not so much mutate an object as export a variable to the > outer scope. > > martin > I don't think so. If one intends to do that, code is a lot clearer if the "outer variable" is explicitly declared. A class of use cases where one needs more than just a value passed along by the enumerator would be traversing a tree with a traverser_state object featuring information like tree_depth, last_visited etc. What I want to say is that the method should be seen from a very general point of view not biased by specialized use cases like summing up the elements in an array. -- henon ------=_Part_35391_31001879.1213135796881 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, Jun 10, 2008 at 11:42 PM, Martin DeMello <martindemello@gmail.com> wrote:
On Tue, Jun 10, 2008 at 10:44 AM, David Flanagan
<david@davidflanagan.com> wrote:
> Martin DeMello wrote:
>>
>>>> ary.each.accumulating(0) {|i, a| a += i}
>>
>>  It should initialize a to 0, mutate it with each pass due to the
>> side effect of a += i, and return the final value. I'm not relying on
>> the return value at all.
>
> Numeric values are not mutable.  a += 1 assigns a new object to a.

Doh! That's a limitation of the method, then, since what we *really*
want to do is not so much mutate an object as export a variable to the
outer scope.

martin

I don't think so. If one intends to do that, code is a lot clearer if the "outer variable" is explicitly declared.

A class of use cases where one needs more than just a value passed along by the enumerator would be traversing a tree with a traverser_state object featuring information like tree_depth, last_visited etc. What I want to say is that the method should be seen from a very general point of view not biased by specialized use cases like summing up the elements in an array.

-- henon

------=_Part_35391_31001879.1213135796881--