[squeak-dev] Collection's #fold: vs #reduce:
leves at elte.hu
Tue Nov 2 21:25:19 UTC 2010
On Tue, 2 Nov 2010, Igor Stasenko wrote:
>> fold: aBinaryBlock
>> "Evaluate the block with the first two elements of the receiver,
>> then with the result of the first evaluation and the next element,
>> and so on. Answer the result of the final evaluation. If the receiver
>> is empty, raise an error. If the receiver has a single element, answer
>> that element."
>> #('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b | a, '
>> ', b]
> just a 0.5 penny..
> wow.. you trying to get a fastest way to fold a string,
> by using a concatenaiton.. i think its not most effective way, at
> least for given example :)
I'm sure nobody is trying that. It's just an example in a comment. And
this thread is not meant to be about performance at all. We have two
implementations of the same thing. To simplify the system, I thought that
I'll remove one of them. Both versions are easy to understand, so I though
it will be a good idea to keep the faster one.
> I think that using streams, it will be much faster.
> (String streamContents: [:s |
> (#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') do: [:each | s
> nextPutAll: each; nextPut: $ ]) allButLast
> Best regards,
> Igor Stasenko AKA sig.
More information about the Squeak-dev