Hi, I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose. However, the current Pharo implementation is less ideal: Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]] The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver. Does anyone have anything against the Moose implementation? Doru -- www.tudorgirba.com "Every thing has its own flow"
I don't have a Pharo at hand now,but if species returns the class of the collection, be carefull with SortedCollection because in that case the sortBlock could not work with the collected objects. On Fri, Nov 1, 2013 at 7:55 PM, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
Hi, flatCollect: is called by Collection>>gather: instead of renaming gather: what we did is add "flatCollect:" to keep compatibility. Take into account that if you change what flatCollect: returns it will affect all the methods that use "gather:" 2013/11/1 Gabriel Cotelli <g.cotelli@gmail.com>
I don't have a Pharo at hand now,but if species returns the class of the collection, be carefull with SortedCollection because in that case the sortBlock could not work with the collected objects.
On Fri, Nov 1, 2013 at 7:55 PM, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species Would it also not be better and more elegant to say self species streamContents: [ :stream | ⦠] ? On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough. Doru On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as
the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns
Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com "Every thing has its own flow"
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote:
Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu>wrote:
BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as
the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns
Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
Actually I am still confused about this, for example, Set new writeStream nextPut: 1; contents does not work, so for which non-sequenceable collections would the #flatCollect: code work ? I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ? On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
On 2013-11-04, at 17:46, Sven Van Caekenberghe <sven@stfx.eu> wrote:
Actually I am still confused about this, for example,
Set new writeStream nextPut: 1; contents
This should actually work the same way as OrderedCollection streamContents: [ :s | s nextPut: 1 ]. or Symbol streamContents: [ :s | s nextPutAll: 'abc' ]
does not work, so for which non-sequenceable collections would the #flatCollect: code work ?
I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ?
On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
Right. I hadn't looked closely enough at the Moose one. Actually, if you dig it a bit deeper, #writeStream isn't defined in the Collection hierarchy until you get to SequenceableCollection in any case, so the Moose version is defined too high. So, if there is a desire for #flatCollect: outside of SequenceableColleciton, then this should work (based on Moose version): Collection>>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" | result | self isEmpty ifTrue: [ ^ self copy ]. result := (self species new: 0). self do: [ :each | result addAll: (aBlock value: each) ]. ^ result SequenceableCollection>>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 ]. ^self species streamContents: [ :stream | self do: [ :each | stream nextPutAll: (aBlock value: each) ] -Chris On Mon, Nov 4, 2013 at 8:46 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote:
Actually I am still confused about this, for example,
Set new writeStream nextPut: 1; contents
does not work, so for which non-sequenceable collections would the #flatCollect: code work ?
I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ?
On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as
the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns
Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
Indeed, it would be great to have a polymorphic message for constructing collections. In the meantime, there are three flatCollect: methods: Collection>>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" | stream | self isEmpty ifTrue: [ ^ self copy ]. stream := (self species new: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents Set>>flatCollect: aBlock ^self flatCollectAsSet: aBlock SortedCollection>>flatCollect: aBlock ^ self flatCollect: aBlock as: OrderedCollection Doru On Mon, Nov 4, 2013 at 6:02 PM, Chris Cunningham <cunningham.cb@gmail.com>wrote:
Right. I hadn't looked closely enough at the Moose one. Actually, if you dig it a bit deeper, #writeStream isn't defined in the Collection hierarchy until you get to SequenceableCollection in any case, so the Moose version is defined too high.
So, if there is a desire for #flatCollect: outside of SequenceableColleciton, then this should work (based on Moose version):
Collection>>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" | result | self isEmpty ifTrue: [ ^ self copy ]. result := (self species new: 0). self do: [ :each | result addAll: (aBlock value: each) ]. ^ result
SequenceableCollection>>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 ]. ^self species streamContents: [ :stream | self do: [ :each | stream nextPutAll: (aBlock value: each) ]
-Chris On Mon, Nov 4, 2013 at 8:46 AM, Sven Van Caekenberghe <sven@stfx.eu>wrote:
Actually I am still confused about this, for example,
Set new writeStream nextPut: 1; contents
does not work, so for which non-sequenceable collections would the #flatCollect: code work ?
I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ?
On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great
as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns
Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com "Every thing has its own flow"
On Nov 4, 2013, at 9:40 PM, Tudor Girba <tudor@tudorgirba.com> wrote:
Indeed, it would be great to have a polymorphic message for constructing collections. In the meantime, there are three flatCollect: methods:
doru do you have an example of what you mean ? For flaCollect: I let you discuss because I'm not enough concentrated but I was when we worked on it long time ago.
Collection>>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"
| stream | self isEmpty ifTrue: [ ^ self copy ]. stream := (self species new: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
Set>>flatCollect: aBlock ^self flatCollectAsSet: aBlock
SortedCollection>>flatCollect: aBlock ^ self flatCollect: aBlock as: OrderedCollection
Doru
On Mon, Nov 4, 2013 at 6:02 PM, Chris Cunningham <cunningham.cb@gmail.com> wrote: Right. I hadn't looked closely enough at the Moose one. Actually, if you dig it a bit deeper, #writeStream isn't defined in the Collection hierarchy until you get to SequenceableCollection in any case, so the Moose version is defined too high.
So, if there is a desire for #flatCollect: outside of SequenceableColleciton, then this should work (based on Moose version):
Collection>>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"
| result | self isEmpty ifTrue: [ ^ self copy ]. result := (self species new: 0). self do: [ :each | result addAll: (aBlock value: each) ]. ^ result
SequenceableCollection>>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 ]. ^self species streamContents: [ :stream | self do: [ :each | stream nextPutAll: (aBlock value: each) ]
-Chris On Mon, Nov 4, 2013 at 8:46 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: Actually I am still confused about this, for example,
Set new writeStream nextPut: 1; contents
does not work, so for which non-sequenceable collections would the #flatCollect: code work ?
I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ?
On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
I meant having what Sven mentioned: Collection>>streamContents: Right now, we only have it in SequeanceableCollection. Doru On Sat, Nov 9, 2013 at 1:27 AM, Stéphane Ducasse <stephane.ducasse@inria.fr>wrote:
On Nov 4, 2013, at 9:40 PM, Tudor Girba <tudor@tudorgirba.com> wrote:
Indeed, it would be great to have a polymorphic message for constructing collections. In the meantime, there are three flatCollect: methods:
doru do you have an example of what you mean ?
For flaCollect: I let you discuss because I'm not enough concentrated but I was when we worked on it long time ago.
Collection>>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" | stream | self isEmpty ifTrue: [ ^ self copy ]. stream := (self species new: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
Set>>flatCollect: aBlock ^self flatCollectAsSet: aBlock
SortedCollection>>flatCollect: aBlock ^ self flatCollect: aBlock as: OrderedCollection
Doru
On Mon, Nov 4, 2013 at 6:02 PM, Chris Cunningham <cunningham.cb@gmail.com>wrote:
Right. I hadn't looked closely enough at the Moose one. Actually, if you dig it a bit deeper, #writeStream isn't defined in the Collection hierarchy until you get to SequenceableCollection in any case, so the Moose version is defined too high.
So, if there is a desire for #flatCollect: outside of SequenceableColleciton, then this should work (based on Moose version):
Collection>>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" | result | self isEmpty ifTrue: [ ^ self copy ]. result := (self species new: 0). self do: [ :each | result addAll: (aBlock value: each) ]. ^ result
SequenceableCollection>>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 ]. ^self species streamContents: [ :stream | self do: [ :each | stream nextPutAll: (aBlock value: each) ]
-Chris On Mon, Nov 4, 2013 at 8:46 AM, Sven Van Caekenberghe <sven@stfx.eu>wrote:
Actually I am still confused about this, for example,
Set new writeStream nextPut: 1; contents
does not work, so for which non-sequenceable collections would the #flatCollect: code work ?
I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ?
On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great
as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always
returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com "Every thing has its own flow"
can you open a bug entry because improving this part would be good. On Nov 9, 2013, at 8:48 AM, Tudor Girba <tudor@tudorgirba.com> wrote:
I meant having what Sven mentioned:
Collection>>streamContents:
Right now, we only have it in SequeanceableCollection.
Doru
On Sat, Nov 9, 2013 at 1:27 AM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
On Nov 4, 2013, at 9:40 PM, Tudor Girba <tudor@tudorgirba.com> wrote:
Indeed, it would be great to have a polymorphic message for constructing collections. In the meantime, there are three flatCollect: methods:
doru do you have an example of what you mean ?
For flaCollect: I let you discuss because I'm not enough concentrated but I was when we worked on it long time ago.
Collection>>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"
| stream | self isEmpty ifTrue: [ ^ self copy ]. stream := (self species new: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
Set>>flatCollect: aBlock ^self flatCollectAsSet: aBlock
SortedCollection>>flatCollect: aBlock ^ self flatCollect: aBlock as: OrderedCollection
Doru
On Mon, Nov 4, 2013 at 6:02 PM, Chris Cunningham <cunningham.cb@gmail.com> wrote: Right. I hadn't looked closely enough at the Moose one. Actually, if you dig it a bit deeper, #writeStream isn't defined in the Collection hierarchy until you get to SequenceableCollection in any case, so the Moose version is defined too high.
So, if there is a desire for #flatCollect: outside of SequenceableColleciton, then this should work (based on Moose version):
Collection>>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"
| result | self isEmpty ifTrue: [ ^ self copy ]. result := (self species new: 0). self do: [ :each | result addAll: (aBlock value: each) ]. ^ result
SequenceableCollection>>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 ]. ^self species streamContents: [ :stream | self do: [ :each | stream nextPutAll: (aBlock value: each) ]
-Chris On Mon, Nov 4, 2013 at 8:46 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: Actually I am still confused about this, for example,
Set new writeStream nextPut: 1; contents
does not work, so for which non-sequenceable collections would the #flatCollect: code work ?
I was thinking that maybe #streamContents: could be put higher up ? If that would not be possible, why not ? And how would the #flatCollect: code then work ?
On 04 Nov 2013, at 17:36, Chris Cunningham <cunningham.cb@gmail.com> wrote:
On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor@tudorgirba.com> wrote: Indeed, it would be more elegant, but streamContents: is only defined in SequeanceableCollection, so it is not generic enough.
So, then use the generic one where it is defined (Collection), and a more specific one that Sven suggested in SequenceableCollection.
-Chris
Doru
On Sat, Nov 2, 2013 at 11:21 AM, Sven Van Caekenberghe <sven@stfx.eu> wrote: BTW, it seems #flatten in 2.0 has become #flattened in 3.0 and that too might needs the #species
Would it also not be better and more elegant to say
self species streamContents: [ :stream | ⦠]
?
On 02 Nov 2013, at 09:50, Sven Van Caekenberghe <sven@stfx.eu> wrote:
On 01 Nov 2013, at 23:55, Tudor Girba <tudor@tudorgirba.com> wrote:
Hi,
I see that Pharo 3.0 has a Collection>>flatCollect:. This is great as the method proved to be very valuable in the context of Moose.
However, the current Pharo implementation is less ideal:
Collection>>flatCollect: aBlock ^ Array streamContents: [:stream | self do: [:ea | stream nextPutAll: (aBlock value: ea)]]
The Moose one is: Collection>>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: 0) writeStream. self do: [ :each | stream nextPutAll: (aBlock value: each) ]. ^ stream contents
The difference is in the type returned. The Pharo one always returns Array, while the Moose one returns a collection of the same species as the receiver.
Sounds right, returning #species.
Does anyone have anything against the Moose implementation?
Doru
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
-- www.tudorgirba.com
"Every thing has its own flow"
participants (7)
-
Camillo Bruni -
Chris Cunningham -
Gabriel Cotelli -
Sebastian Tleye -
Stéphane Ducasse -
Sven Van Caekenberghe -
Tudor Girba