help-smalltalk
[Top][All Lists]
Advanced

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

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


From: Stephen Compall
Subject: [Help-smalltalk] #flatten for seq-collections, #join: for collection classes
Date: Tue, 05 Jun 2007 23:09:01 -0500

address@hidden/smalltalk--backstage--2.2--patch-26

(also attached) adds "flattening" to sequenceable collections, and
"joining" to collection classes (or "withAllWithAll:" :).  These are
more or less equivalent, and both are present because while the
"flatten" operation more naturally dispatches on the collection class of
its result, it is more naturally the responsibility of the collection
being flattened.

Because of the convenience of {}-syntax, flatten is also useful as a
more efficient concatenation operator than #','.  In fact, this is what
I intended it for.

I didn't see "flattening" as something that could be properly done to a
non-Sequenceable collection (out of order, as it were), similarly to how
a non-sequenceable collection cannot be the with: in a
#copyReplaceFrom:to:with:.  Joining is still available on Collection.
If I am mistaken, I can fix it by moving #flatten: to Collection and
adding a #flatten to Collection like:

flatten
    ^self isEmpty ifTrue: [#()]
                  ifFalse: [self flatten: self anyOne species]

The #flatten: method was inspired by the aesthetic of
COMMON-LISP:CONCATENATE
<http://www.lispworks.com/documentation/HyperSpec/Body/f_concat.htm>.

One may note that #flatten shares with #from: an interesting property:
it is designed to be used often with {}-expressions, where such can be
efficiently compiler-optimized with Presource, my Smalltalk
compiler-extension goodie.  If there is interest, I can show how this CL
compiler-macro-like extension can be done, and perhaps improve support
for compiler context-sensitivity (wrt current class, category, and such)
in Presource's RewritingCompile.

-- 
;;; 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: coll-flatten.diff
Description: Text Data

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


reply via email to

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