[Pharo-project] About nextPut:
Hi damien I was looking at the following code Sequenceable>>flatCollect: aBlock "Evaluate aBlock for each of the receiver's elements and answer the list of all resulting values flatten one level. Assumes that aBlock returns some kind of collection for each element. Equivalent to the lisp's mapcan" "original written by a. Kuhn and released under MIT" | stream | self isEmpty ifTrue: [^self copy ]. stream := (self species new: self size) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^stream contents and if we send the message to a set we get a problem because WriteStream>>nextPut: anObject "Primitive. Insert the argument at the next position in the Stream represented by the receiver. Fail if the collection of this stream is not an Array or a String. Fail if the stream is positioned at its end, or if the position is out of bounds in the collection. Fail if the argument is not of the right type for the collection. Optional. See Object documentation whatIsAPrimitive." <primitive: 66> ((collection class == ByteString) and: [ anObject isCharacter and:[anObject isOctetCharacter not]]) ifTrue: [ collection := (WideString from: collection). ^self nextPut: anObject. ]. position >= writeLimit ifTrue: [^ self pastEndPut: anObject] ifFalse: [position := position + 1. ^collection at: position put: anObject] so the solution is to specialize flatCollect: on Set flatCollect: aBlock "Evaluate aBlock for each of the receiver's elements and answer the union of all resulting values. Assumes that aBlock returns some kind of collection for each element." | set | self isEmpty ifTrue: [^self copy]. set := Set new. self do: [ :each | set addAll: (aBlock value: each) ]. ^set Now I was wondering if there is way to avoid that. Stef
On Thu, Oct 16, 2008 at 3:52 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
Now I was wondering if there is way to avoid that.
A stream can be seen as a pointer between past and future sequence values. Which means there must be a way to index the element in the collection of data and this is not the case with Sets. In the case of #flatCollect:, you do not want a stream, you want a generic way to add data to a collection of elements. I don't see any generic way to do this. -- Damien Cassou Peter von der Ahé: «I'm beginning to see why Gilad wished us good luck». (http://blogs.sun.com/ahe/entry/override_snafu)
On Oct 17, 2008, at 2:55 PM, Damien Cassou wrote:
On Thu, Oct 16, 2008 at 3:52 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
Now I was wondering if there is way to avoid that.
A stream can be seen as a pointer between past and future sequence values. Which means there must be a way to index the element in the collection of data and this is not the case with Sets. In the case of #flatCollect:, you do not want a stream, you want a generic way to add data to a collection of elements. I don't see any generic way to do this.
why a stream is not adequate. for me this a collection of elements.
-- Damien Cassou Peter von der Ahé: «I'm beginning to see why Gilad wished us good luck». (http://blogs.sun.com/ahe/entry/override_snafu) _______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
On Fri, Oct 17, 2008 at 8:47 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
why a stream is not adequate. for me this a collection of elements.
"a collection of elements" is named a collection in Smalltalk :-). If you want a growable collection, use a growable collection. Streams should be use to iterate over a sequence of elements (read streams) or write elements to a backing store (write streams). -- Damien Cassou Peter von der Ahé: «I'm beginning to see why Gilad wished us good luck». (http://blogs.sun.com/ahe/entry/override_snafu)
participants (2)
-
Damien Cassou -
Stéphane Ducasse