Streaming on all collections

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Streaming on all collections

Damien Cassou-3
Hello,

why isn't it possible to stream over OrderedCollections ?


stream := WriteStream on: OrderedCollection new.
stream nextPut: $a

this generates a 'attempt to index non-existent element in an ordered
collection'


The documentation of WriteStream>>nextPut: says :

«
Fail if the collection of this stream is not an Array or a String.
»

Can somebody explain me why it is forbidden ?

--
Damien Cassou


Reply | Threaded
Open this post in threaded view
|

Re: Streaming on all collections

Jon Hylands
On Sun, 11 Feb 2007 18:00:10 +0100, Damien Cassou
<[hidden email]> wrote:

> why isn't it possible to stream over OrderedCollections ?

You can only add elements to an OrderedCollection by using #add: (or one of
its variants). WriteStream assumes that if you send #new: to the class of
the collection it is streaming over, you can then say #at: index put:
value, where index <= the collections size.

If you want to do this, use Array.

stream := WriteStream on: Array new.
stream nextPut: $a

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog

Reply | Threaded
Open this post in threaded view
|

Re: Streaming on all collections

Damien Cassou-3
Jon Hylands a écrit :
> On Sun, 11 Feb 2007 18:00:10 +0100, Damien Cassou
> <[hidden email]> wrote:
>
>> why isn't it possible to stream over OrderedCollections ?
>
> You can only add elements to an OrderedCollection by using #add: (or one of
> its variants). WriteStream assumes that if you send #new: to the class of
> the collection it is streaming over, you can then say #at: index put:
> value, where index <= the collections size.

Sorry, I was not clear enough. I'm not talking about implementation
details, but about semantic of streams. I don't understand why
WriteStreams would not deal with all kind of sequenceable collections.

--
Damien Cassou

Reply | Threaded
Open this post in threaded view
|

Re: Streaming on all collections

Klaus D. Witzel
Hi Damien,

on Sun, 11 Feb 2007 21:11:22 +0100, you wrote:

> Jon Hylands a écrit :
>> On Sun, 11 Feb 2007 18:00:10 +0100, Damien Cassou
>> <[hidden email]> wrote:
>>
>>> why isn't it possible to stream over OrderedCollections ?
>>  You can only add elements to an OrderedCollection by using #add: (or  
>> one of
>> its variants). WriteStream assumes that if you send #new: to the class  
>> of
>> the collection it is streaming over, you can then say #at: index put:
>> value, where index <= the collections size.
>
> Sorry, I was not clear enough. I'm not talking about implementation  
> details, but about semantic of streams. I don't understand why  
> WriteStreams would not deal with all kind of sequenceable collections.

You are right, insofar as the class comments of Stream,  
PositionableStream, ReadStream and WriteStream talk about a *sequence* (of  
stream operations, IMO).

But only (sub-)instances of ArrayedCollection provide a *fixed* range of  
integers as external keys.

In contrast to that, OrderedCollection has also integers as externals key,  
but not a fixed range of them: the range *varies* with the size of the  
instance.

A simulation of ArrayedCollection can be

  (1 to: 6) asOrderedCollection writeStream nextPutAll: 'string'; contents

/Klaus

>



Reply | Threaded
Open this post in threaded view
|

RE: Streaming on all collections

Alan L. Lovejoy
In reply to this post by Damien Cassou-3
<Damien Cassou>
why isn't it possible to stream over OrderedCollections ?

stream := WriteStream on: OrderedCollection new.
stream nextPut: $a

this generates a 'attempt to index non-existent element in an ordered
collection'
</Damien Cassou>

In VisualWorks, the following code "does the right thing":

(WriteStream on: OrderedCollection new)
        nextPut: $H;
        nextPut: $e;
        nextPut: $l;
        nextPut: $l;
        nextPut: $o;
        nextPut: $ ;
        nextPut: $W;
        nextPut: $o;
        nextPut: $r;
        nextPut: $l;
        nextPut: $d;
        nextPut: $!;
        contents

I fail to grok why Squeak should not do likewise.

--Alan

P.S. Dolphin works (or rather, fails to work) just like Squeak in this
regard.



Reply | Threaded
Open this post in threaded view
|

Re: Streaming on all collections

Jon Hylands
In reply to this post by Damien Cassou-3
On Sun, 11 Feb 2007 12:46:54 -0800, "Alan Lovejoy"
<[hidden email]> wrote:

> In VisualWorks, the following code "does the right thing":

The reason it works in VW is that #growToAtLeast: sets lastIndex to the
size the collection has grown, whereas in Squeak it keeps it at the current
position.

Later,
Jon

--------------------------------------------------------------
   Jon Hylands      [hidden email]      http://www.huv.com/jon

  Project: Micro Raptor (Small Biped Velociraptor Robot)
           http://www.huv.com/blog

Reply | Threaded
Open this post in threaded view
|

Re: Streaming on all collections

Philippe Marschall
In reply to this post by Damien Cassou-3
2007/2/11, Alan Lovejoy <[hidden email]>:

> <Damien Cassou>
> why isn't it possible to stream over OrderedCollections ?
>
> stream := WriteStream on: OrderedCollection new.
> stream nextPut: $a
>
> this generates a 'attempt to index non-existent element in an ordered
> collection'
> </Damien Cassou>
>
> In VisualWorks, the following code "does the right thing":
>
> (WriteStream on: OrderedCollection new)
>         nextPut: $H;
>         nextPut: $e;
>         nextPut: $l;
>         nextPut: $l;
>         nextPut: $o;
>         nextPut: $ ;
>         nextPut: $W;
>         nextPut: $o;
>         nextPut: $r;
>         nextPut: $l;
>         nextPut: $d;
>         nextPut: $!;
>         contents
>
> I fail to grok why Squeak should not do likewise.
>
> --Alan
>
> P.S. Dolphin works (or rather, fails to work) just like Squeak in this
> regard.

The same as GST.  Smalltalk/X however "does the right thing" as well.

Philippe

Reply | Threaded
Open this post in threaded view
|

RE: Streaming on all collections

Alan L. Lovejoy
In reply to this post by Jon Hylands
<Jon Hylands>
The reason it works in VW is that #growToAtLeast: sets lastIndex to the size
the collection has grown, whereas in Squeak it keeps it at the current
position.
<Jon Hylands>

I'd explain it differently: Around the time of VW 1.0, someone at ParcPlace
put a lot of effort into rigourously making the Collection classes
intelligently distinguish between the concept of a collection's #size (its
logical size, the number of elements it contains logically) a a collection's
#capacity (its physical size, the number of slots it has allocated in which
elements could be stored.)

In VW, a Collection responds to #growToAtLeast: by increasing its logical
size.  In Squeak, a Collection responds to #growToAtLeast: by increasing its
physical size--which in some, but not all cases, also increases the
Collection's logical size.  Unfortunately, for OrderedCollections the
distinction between logical size and physical size (capacity) actually
matters.

--Alan



Reply | Threaded
Open this post in threaded view
|

Re: Streaming on all collections

Klaus D. Witzel
In reply to this post by Jon Hylands
Thank you Alan, good background info.

Separating #capacity from #size is very intuitive.

/Klaus

On Sun, 11 Feb 2007 22:49:29 +0100, Alan Lovejoy wrote:

> <Jon Hylands>
> The reason it works in VW is that #growToAtLeast: sets lastIndex to the  
> size
> the collection has grown, whereas in Squeak it keeps it at the current
> position.
> <Jon Hylands>
>
> I'd explain it differently: Around the time of VW 1.0, someone at  
> ParcPlace
> put a lot of effort into rigourously making the Collection classes
> intelligently distinguish between the concept of a collection's #size  
> (its
> logical size, the number of elements it contains logically) a a  
> collection's
> #capacity (its physical size, the number of slots it has allocated in  
> which
> elements could be stored.)
>
> In VW, a Collection responds to #growToAtLeast: by increasing its logical
> size.  In Squeak, a Collection responds to #growToAtLeast: by increasing  
> its
> physical size--which in some, but not all cases, also increases the
> Collection's logical size.  Unfortunately, for OrderedCollections the
> distinction between logical size and physical size (capacity) actually
> matters.
>
> --Alan
>
>
>
>