[ANSI-Smalltalk] Behaviour of #collect:
Peter van Rooijen
peter at vanrooijen.com
Tue Sep 23 08:16:59 BST 2008
On Tue, 23 Sep 2008 05:40:12 +0200, Richard A. O'Keefe <ok at cs.otago.ac.nz>
wrote:
>
> On 23 Sep 2008, at 4:40 am, Eliot Miranda wrote:
>
>> what about
>> Collection class collect: aBlock from: aCollection
>> e.g.
>> Array collect: [:each | each codePoint] from: 'abc'
>> ?
>>
> Because there isn't any #collect:from: in the Smalltalks
> I use.
I think that Eliot was simply proposing another name, that speaks better
to him. So whether or not this name appears anywhere else might not be of
overriding importance. #collect:from: appeals more to me, too. I think I'd
remember that easily, when I run into a myCollection collect: ... that I
realize (or find out) won't work as I want.
I think you have identified 2 separate and valid issues:
1) Would adding #withAll:collect: to every class that has #withAll:
be considered a good move?
Are you talking about a general method on Collection class that would be
essentially this?
Collection class>>#collect: monadicBlock from: collection
| elements |
elements := OrderedCollection new
.
collection do: [:x | elements add: (monadicBlock value: x)]
.
^self withAll: elements
(warning: untested code, so contains bugs)
Where applicable, subclasses of Collection would then contain optimized
(specifically, avoiding the creation of the intermediate elements
collection), but otherwise equivalent implementations.
Did I understand you correctly?
2) What exactly should #collect: do when sent to an object that
belongs to a class whose instances cannot hold the results that
the block returns?
This seems like a hard one, practically. Whatever current implementations
do, would be hard to change. If we want a standard that is accepted, we
probably shouldn't try to go that way. But isn't the upside that the
problem becomes kinda moot with the addition of the class method discussed
above?
> I've already mentioned the operation I added,
> which is named consistently with the ANSI methods, namely
> <class> withAll: collection collect: aBlock
> This also goes with
> <collection> addAll: collection collect: aBlock
>
> #withAll:collect: is nice, and I'd like to see it in the
> new standard, but it's long winded and it's limiting.
I think your idea is valid (and I don't see why it would be long winded
and limiting). I like Eliot's proposed name better. Does someone see a
problem with this? If not, I'd like to propose to add this to the new
standard.
Cheers, Peter
> _______________________________________________
> ANSI-Smalltalk mailing list
> ANSI-Smalltalk at lists.openskills.org
> http://lists.openskills.org/cgi-bin/mailman/listinfo/ansi-smalltalk
--
Peter van Rooijen
Esstede 19
1112 EP Diemen
m: 06-2854 2048
More information about the ANSI-Smalltalk
mailing list