[Pharo-dev] flatCollect:

Chris Cunningham cunningham.cb at gmail.com
Mon Nov 4 11:36:03 EST 2013


On Sat, Nov 2, 2013 at 3:52 AM, Tudor Girba <tudor at 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 at 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 at stfx.eu> wrote:
>>
>> >
>> > On 01 Nov 2013, at 23:55, Tudor Girba <tudor at 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"
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131104/2884f3a7/attachment-0002.html>


More information about the Pharo-dev mailing list