[squeak-dev] #in: for collections
cputney at wiresong.ca
Sat Dec 26 15:08:19 UTC 2009
On 2009-12-26, at 3:20 AM, Bert Freudenberg wrote:
> ... however, #in: only works with single-argument blocks. Making #in: work with multi-arg blocks is of course possible but feels not right either. Does anybody have an idea for a better selector? Or another idea for how to code this?
I've occasionally mused that #do: and #in: ought to be the inverse of each other. Object>>do: would behave like #in: and Collection>>in: would evaluate the block with 'self anyOne' as the parameter. With multi-arg blocks it could supply more elements, as in your third example. On UndefinedObject, both methods would do nothing. This would probably break a lot of code, so it's not really feasible now, but if I could think of some other selectors that made sense...
Your idea here strikes me as similar to Ruby's splat operator, or the way functional languages deal with lists by recursively splitting them into head and tail. It feels like a really useful idiom, and it would be nice to have it in Smalltalk, the same way we have #ifTrue:ifFalse: rather than actual syntax for booleans.
> In my example, "#getChannels" is an external library call returning an array, this is the code that converts the raw result into a proper object structure. You can argue that low-level code like this is okay to look like (2) but I'd say it doesn't have to ;)
My first thought on reading the code was "Why the heck is he passing around arrays like that? Make it an object!" Preaching to the choir, I guess. :-)
More information about the Squeak-dev