[Cuis-dev] Missing ifEmpty: variants for collections
Facundo Javier Gelatti
javiergelatti at gmail.com
Mon Sep 21 16:35:19 PDT 2026
Hello!
I attach a change set to add some useful methods to the collections. In
particular, missing "ifEmpty" cases, like #sumIfEmpty: or #max:ifEmpty:. I
also attach another change set for the CollectionsTests package, to add
some tests for these changes.
Here I leave some notes regarding the changes, to help during the review:
* For the message #anyOne, the raised exceptions are different now. For
example, "(1 to: 0) anyOne" used to raise "subscript is out of bounds: 1",
and now raises "this collection is empty" (the same thing happens to
"RunArray new anyOne"). I think this is an improvement, although I mention
it explicitly just in case.
* One case we should validate is with Heap, because "Heap new anyOne" used
to return "nil", but now raises "this collection is empty". As the size of
the heap is 0 and #isEmpty returns true, I think the change is in the
correct direction.
* I followed the convention of evaluating "emptyBlockOrValue
valueWithPossibleArgument: self" in the empty cases, like
Collection>>#ifEmpty: does. This sometimes changes the behavior a bit, by
being more permissive. For an interesting case regarding this, see for
example the change in #average:ifEmpty:.
* I found that Collection>>#max: and #min: skip leading nils. For example:
"#(nil 3) max: [:x | x]" answers 3, but "#(3 nil) max: [:x | x]" raises an
exception. The code is very explicit about this, so I guess this is (or at
some point was) the expected behavior. I left this behavior as it was, but
I'm curious if this is something that could be simplified (my expectation
would have been for both examples to raise an exception). On the other
hand, in case it's needed, it'd be nice to have at least one test
documenting it.
* I left the cases where nil is returned by default as they are (e.g.
#detectMax:/#detectMin:), because I'm thinking of this as a change closer
to a refactor. But we might consider revisiting those defaults, and make
them raise an exception instead, to be consistent with what's done in other
cases.
Cheers!
Facu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20260921/2862a605/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Collection-ifEmptyMethods-Tests-FJG.001.cs.st
Type: application/vnd.sailingtracker.track
Size: 7087 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20260921/2862a605/attachment-0002.st>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Collection-ifEmptyMethods-FJG.001.cs.st
Type: application/vnd.sailingtracker.track
Size: 12903 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20260921/2862a605/attachment-0003.st>
More information about the Cuis-dev
mailing list