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

_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply via email to