You are looking for doesNotUnderstand: If reduce is an object that does not implement + , but you send it anyway, then its doesNotUnderstand: method will be called instead. This usually just causes an error. But you can override the default value and access the + selector and another argument and do whatever you like with them.
For simplicity, create a reduce class. On the class side, define a method:
doesNotUnderstand: aMessage ^aMessage argument reduce: aMessage selector
Then you can use it as follows:
Reduce + (
which in the Squeak workspace answers 32, as expected.
This works because * already implemented for collections with suitable semantics.
Alternatively, add the ApplyToAll class using this class method:
doesNotUnderstand: aMessage ^aMessage argument collect: [:e | e reduce: aMessage selector]
and also add this method to the SequenceableCollection :
transposed ^self first withIndexCollect: [:c :i | self collect: [:r | r at: i]]
Then you can write
Reduce + (ApplyToAll *
which is close to your original idea.
Bert freudenberg
source share