help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] #flatten for seq-collections, #join: for collection


From: Stephen Compall
Subject: Re: [Help-smalltalk] #flatten for seq-collections, #join: for collection classes
Date: Wed, 06 Jun 2007 00:53:33 -0500

On Wed, 2007-06-06 at 07:03 +0200, Paolo Bonzini wrote:
> Very cool!  However, what about just calling #join also the 
> instance-side #flatten, and omitting the instance-side #flatten: (which 
> is simpler to do with a class-side #join:)?

While coming up with a name for the instance-side (first #concatenate,
then #join as it happens, then #flatten), I thought it sounded too
general.  How about #joinAll?

> #flatten is more for something like
> 
>    #((1 2) 3 (4 5)) => 1 2 3 4 5

More like (with Presource)

(#((1 2) 3 (4 5)) collect: #mklist sendingBlock) flatten

> This can be done by adding a #flattenInto: method that does "add: self" 
> or "self do: [ :each | each flattenInto: dest ]" (the latter for 
> collections and streams).
> 
> As a nitpick, #inject:into: is slow -- streams might be faster

I can eliminate the second #inject:into: in ArrayColl.st quite easily if
you are comfortable with

| newInst start |
newInst := ...
start := 1.
aCollection do: [:subColl |
    "this start := thing right here...the stack is my temporary storage :)"
    subColl replaceFrom: start to: (start := start + subColl size) - 1
            with: subColl].
^newInst

Maybe I'm duplicating a little of what WriteStream does, but I see the
main benefit of it as when tracking position isn't so trivial or
resizing might be necessary.

> and #fold: is nicer anyway for the size computation.

like "(aCollection collect: [:each | each size]) fold: [:a :b | a +
b]" ?

-- 
;;; Stephen Compall ** http://scompall.nocandysw.com/blog **
Failure to imagine vast possibilities usually stems from a lack of
imagination, not a lack of possibility.

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]