[squeak-dev] another example of #reduce:/#fold: (Re: Collection>>sum
implementation)
Paolo Bonzini
bonzini at gnu.org
Tue Aug 12 15:14:25 UTC 2008
> This is a bit like the #join: debate that comes up every now and then
> when a Ruby/Python/Perl ex-pat asks how it is that we don't have a
> method for joining an array of strings into a single string. The short
> answer is that it's because #join: only makes sense when all the
> elements of a collection are strings, and therefore, it's bad design.
I disagree. Indeed, if you think of #join: as
SequenceableCollection>>join: sep
^self fold: [ :a :b | a, sep, b ]
(well, right, if the collection is empty the behavior is different), it
becomes obvious to have an optimized implementation return an object of
class "self first species".
Paolo
More information about the Squeak-dev
mailing list
|