unintuitive select

Brian Rice water at tunes.org
Sat Jul 3 13:01:04 PDT 2004


On Jul 3, 2004, at 12:04 PM, Brian Rice wrote:

> No, it's a feature. It's part of ANSI Smalltalk that select: and 
> reject: don't re-position elements. Dictionaries and other Mappings 
> work the same way.
>
> If you want just the selected elements, you can do:
>
> ({1. 2. 3} reader select: [| :a | a > 2]) contents.

For those of you who aren't familiar with this idiom (probably nearly 
everybody), it's first creating a ReadStream on the sequence 
(specialized to whatever type you request from, here an array), and 
collect: and select: are defined on ReadStreams to return another 
ReadStream (a FilterStream and CollectStream, respectively) which 
returns each result when you call "next" on it. The Filter just 
searches and returns the next matching element, and the Collector just 
applies the block to each element from the source and returns the 
result. This is about two screenfuls of code near the end of 
src/stream.slate.

Basically, Streams are Sequences without position (handy if you don't 
care whether some data source is infinite or not, or ordered or not), 
which is why they "forget" position whereas Sequence's select: doesn't 
"forget". "contents" of course gathers the elements into an actual 
concrete collection. There was a big discussion on the Squeak list 
about whether Sequence's select: should "forget", and it's generally 
safer in terms of the type/behavior of objects for it not to forget. 
Plus, you can actually use that information later, so the extra two 
steps to leave the information behind seems not too bad a cost.

It may actually be nice to make some selectors for this "forgetting 
select:" to encapsulate "(x reader select: []) contents", but I can't 
think of a decent name for such a thing.

It just occurred to me that there's no reject: for them. I'll add that 
since there's an easy definition.

> On Jul 3, 2004, at 7:33 AM, John Leuner wrote:
>
>> Slate 1>  {1.2.3.} select: [ | :a | a > 2 ].
>> {Nil. 3}
>>
>> {1.2.3.4.5.6.7.8.9.}  select: [ | :a | a > 2 ].
>> {Nil. 3.4. 5.6. 7.8.
>>         9}
>
> THIS, on the other hand, is you confusing the parser. :D Liberal use 
> of periods between digits without separators is liable to make it 
> think you want Floats and not Integers.

--
Brian T. Rice
LOGOS Research and Development
http://tunes.org/~water/




More information about the Slate mailing list