On Mon, 20 Aug 2012, [hidden email] wrote:
> Frank Shearar uploaded a new version of Collections to project The Inbox: > http://source.squeak.org/inbox/Collections-fbs.489.mcz > > ==================== Summary ==================== > > Name: Collections-fbs.489 > Author: fbs > Time: 20 August 2012, 4:51:36.018 pm > UUID: d1834809-65b5-46c0-9b26-fea7ec04a946 > Ancestors: Collections-ul.488 > > SequenceableCollection >> flatten turns a nested SequenceableCollection - a collection of collections of ... - into a simple collection. Thus, #(1 (2 3) (4)) flatten = #(1 2 3 4). it once, see Collections-klub.130.mcz in the Treated Inbox. I think our implementation is better, because - it uses #flattened as selector - it uses double dispatch - it works well with Strings (try #('foo' ('bar')) flatten ) But I wouldn't push either version to the Trunk now. I could imagine an implementation which does the iteration with double dispatch and uses a separate method for Array creation. Btw, I usually use #gather: when I need to flatten a level on a collection. Deeper than two-levels collection structures are pretty rare in Smalltalk. I guess this is why there's no #flatten yet. Levente > > =============== Diff against Collections-ul.488 =============== > > Item was added: > + ----- Method: SequenceableCollection>>flatten (in category 'copying') ----- > + flatten > + | recur stream | > + stream := WriteStream on: (Array new: self size). "A lower bound on the final stream count." > + recur := [:coll | coll > + do: [:each | > + each isCollection > + ifTrue: [recur value: each] > + ifFalse: [stream nextPut: each]]]. > + recur value: self. > + ^ stream contents.! > > > |
On 20 August 2012 21:54, Levente Uzonyi <[hidden email]> wrote:
> On Mon, 20 Aug 2012, [hidden email] wrote: > >> Frank Shearar uploaded a new version of Collections to project The Inbox: >> http://source.squeak.org/inbox/Collections-fbs.489.mcz >> >> ==================== Summary ==================== >> >> Name: Collections-fbs.489 >> Author: fbs >> Time: 20 August 2012, 4:51:36.018 pm >> UUID: d1834809-65b5-46c0-9b26-fea7ec04a946 >> Ancestors: Collections-ul.488 >> >> SequenceableCollection >> flatten turns a nested SequenceableCollection - >> a collection of collections of ... - into a simple collection. Thus, #(1 (2 >> 3) (4)) flatten = #(1 2 3 4). > > > #flatten was discussed several times, even we (Balázs and me) implemented it > once, see Collections-klub.130.mcz in the Treated Inbox. I think our > implementation is better, because > - it uses #flattened as selector > - it uses double dispatch > - it works well with Strings (try #('foo' ('bar')) flatten ) > But I wouldn't push either version to the Trunk now. I could imagine an > implementation which does the iteration with double dispatch and uses a > separate method for Array creation. OK. I didn't mean to suggest that #flatten was useful only for Arrays. They're just a convenient thing to use for examples: literal syntax. I hadn't really thought of collections of Strings. I'm not sure why double dispatch is particularly useful, unless you're thinking of being able to flatten arbitrary objects - turning a Person into {#name. 'Frank'}. > Btw, I usually use #gather: when I need to flatten a level on a collection. > Deeper than two-levels collection structures are pretty rare in Smalltalk. I > guess this is why there's no #flatten yet. I was using a two-level collection, so I reckon I'll just remember #gather:. More convenient than using (foo collect: #something) inject: Array new into: #, all the time, which is what drove this implementation in the first place. frank > Levente > >> >> =============== Diff against Collections-ul.488 =============== >> >> Item was added: >> + ----- Method: SequenceableCollection>>flatten (in category 'copying') >> ----- >> + flatten >> + | recur stream | >> + stream := WriteStream on: (Array new: self size). "A lower bound >> on the final stream count." >> + recur := [:coll | coll >> + do: [:each | >> + each isCollection >> + ifTrue: [recur value: each] >> + ifFalse: [stream nextPut: each]]]. >> + recur value: self. >> + ^ stream contents.! >> >> > > > |
On Mon, Aug 20, 2012 at 2:14 PM, Frank Shearar <[hidden email]> wrote: OK. I didn't mean to suggest that #flatten was useful only for Arrays. Strings are collections of characters, so Levente's example of #('foo' ('bar')) flatten would give you #($f $o $o $b $a $r). That's probably not what you wanted. Double-dispatch lets String opt out of flattening, so you'd get $('foo' 'bar').
Colin
|
On 20 August 2012 22:18, Colin Putney <[hidden email]> wrote:
> > > On Mon, Aug 20, 2012 at 2:14 PM, Frank Shearar <[hidden email]> > wrote: > >> OK. I didn't mean to suggest that #flatten was useful only for Arrays. >> They're just a convenient thing to use for examples: literal syntax. I >> hadn't really thought of collections of Strings. I'm not sure why >> double dispatch is particularly useful, unless you're thinking of >> being able to flatten arbitrary objects - turning a Person into >> {#name. 'Frank'}. > > > Strings are collections of characters, so Levente's example of #('foo' > ('bar')) flatten would give you #($f $o $o $b $a $r). That's probably not > what you wanted. Double-dispatch lets String opt out of flattening, so you'd > get $('foo' 'bar'). Right. Mainly my brain stopped working when I tried to imagine a String that contained a String. That, clearly, doesn't make much sense. frank > Colin > > > > > |
In reply to this post by Frank Shearar-3
On Mon, 20 Aug 2012, Frank Shearar wrote:
> On 20 August 2012 21:54, Levente Uzonyi <[hidden email]> wrote: >> On Mon, 20 Aug 2012, [hidden email] wrote: >> >>> Frank Shearar uploaded a new version of Collections to project The Inbox: >>> http://source.squeak.org/inbox/Collections-fbs.489.mcz >>> >>> ==================== Summary ==================== >>> >>> Name: Collections-fbs.489 >>> Author: fbs >>> Time: 20 August 2012, 4:51:36.018 pm >>> UUID: d1834809-65b5-46c0-9b26-fea7ec04a946 >>> Ancestors: Collections-ul.488 >>> >>> SequenceableCollection >> flatten turns a nested SequenceableCollection - >>> a collection of collections of ... - into a simple collection. Thus, #(1 (2 >>> 3) (4)) flatten = #(1 2 3 4). >> >> >> #flatten was discussed several times, even we (Balázs and me) implemented it >> once, see Collections-klub.130.mcz in the Treated Inbox. I think our >> implementation is better, because >> - it uses #flattened as selector >> - it uses double dispatch >> - it works well with Strings (try #('foo' ('bar')) flatten ) >> But I wouldn't push either version to the Trunk now. I could imagine an >> implementation which does the iteration with double dispatch and uses a >> separate method for Array creation. > > OK. I didn't mean to suggest that #flatten was useful only for Arrays. > They're just a convenient thing to use for examples: literal syntax. I > hadn't really thought of collections of Strings. I'm not sure why > double dispatch is particularly useful, unless you're thinking of > being able to flatten arbitrary objects - turning a Person into > {#name. 'Frank'}. very simple (one-liner) methods in our implementation. Levente > >> Btw, I usually use #gather: when I need to flatten a level on a collection. >> Deeper than two-levels collection structures are pretty rare in Smalltalk. I >> guess this is why there's no #flatten yet. > > I was using a two-level collection, so I reckon I'll just remember > #gather:. More convenient than using (foo collect: #something) inject: > Array new into: #, all the time, which is what drove this > implementation in the first place. > > frank > >> Levente >> >>> >>> =============== Diff against Collections-ul.488 =============== >>> >>> Item was added: >>> + ----- Method: SequenceableCollection>>flatten (in category 'copying') >>> ----- >>> + flatten >>> + | recur stream | >>> + stream := WriteStream on: (Array new: self size). "A lower bound >>> on the final stream count." >>> + recur := [:coll | coll >>> + do: [:each | >>> + each isCollection >>> + ifTrue: [recur value: each] >>> + ifFalse: [stream nextPut: each]]]. >>> + recur value: self. >>> + ^ stream contents.! >>> >>> >> >> >> > > |
In reply to this post by Levente Uzonyi-2
On 2012-08-20, at 22:54, Levente Uzonyi wrote:
> I usually use #gather: when I need to flatten a level on a collection. Deeper than two-levels collection structures are pretty rare in Smalltalk. I guess this is why there's no #flatten yet. Me too. Works fine with collections of collections of Strings, for example. It should just be moved into the Collections package properly ... - Bert - |
I assume it has to go from the method protocol '*packageinfo-base' to
'filter streaming'? --Hannes On 8/21/12, Bert Freudenberg <[hidden email]> wrote: > On 2012-08-20, at 22:54, Levente Uzonyi wrote: > >> I usually use #gather: when I need to flatten a level on a collection. >> Deeper than two-levels collection structures are pretty rare in Smalltalk. >> I guess this is why there's no #flatten yet. > > > Me too. Works fine with collections of collections of Strings, for example. > > It should just be moved into the Collections package properly ... > > - Bert - > > > > Collection--gather.png (59K) Download Attachment |
Free forum by Nabble | Edit this page |