Collection>>flatten - looking for a better name 
Author Message
 Collection>>flatten - looking for a better name

Dear All,

I am looking for nice, short and meaningful names for a couple of new methods
in Collection. This is what they do:

Imagine that you have a collection of cars and you need to get a collection
of all their wheels. I can write the following:
    ^cars flatCollect: [:e | e wheels]

Where Collection>>flatCollect: aBlock is
    ^(self collect: aBlock) flatten.

In its turn, the #flatten method creates a flat collection from a multiple-
level one. That is, #(1 #(2 3) #(4 5 6) 7) turns into #(1 2 3 4 5 6 7).

The functionality is very simple, and I was surprised that it is not
present in the system classes (at least not in VS 3.1). Anyway,
these are the best method names I've come up with. Since English is not my
first language, I could have missed better names. Will appreciate
any sound advice.

If you are curious, this is the code :

Collection>>flatten
^self inject: self class new into: [:coll :each| coll, (each flatten)]

Object>>flatten
String>>flatten             "It's a Collection subclass, but a String
                            should stay as a whole"
CompiledMethod>>flatten     "It's a Collection subclass too "
    ^Array with: self

This does not work well with Sets and probably something else.

Thanks,
Simon

P.S. The idea was somewhat inspired by the comma-comma example.

 _________                Simon (Vsevolod ILyushchenko)
|       x |

|_________|   (For all normal mail)

Disclaimer: This is not me. This is just my mailer talking to your mailer...



Thu, 29 Oct 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:

> I am looking for nice, short and meaningful names for a couple of new methods
> in Collection. This is what they do:

> Imagine that you have a collection of cars and you need to get a collection
> of all their wheels. I can write the following:
>     ^cars flatCollect: [:e | e wheels]

> Where Collection>>flatCollect: aBlock is
>     ^(self collect: aBlock) flatten.

> In its turn, the #flatten method creates a flat collection from a multiple-
> level one. That is, #(1 #(2 3) #(4 5 6) 7) turns into #(1 2 3 4 5 6 7).

> The functionality is very simple, and I was surprised that it is not
> present in the system classes (at least not in VS 3.1). Anyway,
> these are the best method names I've come up with. Since English is not my
> first language, I could have missed better names. Will appreciate
> any sound advice.

> If you are curious, this is the code :

> Collection>>flatten
> ^self inject: self class new into: [:coll :each| coll, (each flatten)]

> Object>>flatten
> String>>flatten             "It's a Collection subclass, but a String
>                             should stay as a whole"
> CompiledMethod>>flatten     "It's a Collection subclass too "
>     ^Array with: self

> This does not work well with Sets and probably something else.

> Thanks,
> Simon

> P.S. The idea was somewhat inspired by the comma-comma example.

this is kind of reminding me of the deepCopy issue ...

can't say English is my native language, but let me try:

first suggestion - how about #asUnnestedCollection ?
or, if we try to verb-lize it: #unnest

second suggestion - like the deepCopy famous debate, how about
parameterizing the level of un-nesting (or de-nesting...english anyone?).
so how about #unnestToLevel: depth. and off course you can have a
recursive method or block where you decrement the depth each time ;-)

third suggestion - while we're at it, how about using a stream instead of
concatenating. let's say, for efficiency sake.
so how about #unnestToLevel: depth into: aStream
now, you can open the stream, get its contents, and close it yourself
where you send the message (dirty & redundant IMHO), or let the reciever
handle it by - either #unnestToLevel: depth into: nil and take it from
there (again, dirty), or send #unnestToLevel: depth and let the receiver
open aStream , send #unnestToLevel: depth into: aStream (now private
method), close aStream , and return aStream contents.
and voila, the implementation was encapsulated.

I'm sure you can take it further from here and indulge with aesthetics
;-)

any thoughts anyone?

p.s. this idea of unnesting isn't limited to collections. For example,
when you want the bill-of-materials for deeply composited objects, nodes
of nested lists, and leaves of trees. so I wouldn't implement it only for
collection, or I would make it clear through something like  
#asUnnestedCollection.

Benny Sadeh



Thu, 29 Oct 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

: Dear All,

: I am looking for nice, short and meaningful names for a couple of new methods
: in Collection. This is what they do:

: Imagine that you have a collection of cars and you need to get a collection
: of all their wheels. I can write the following:
:     ^cars flatCollect: [:e | e wheels]

: Where Collection>>flatCollect: aBlock is
:     ^(self collect: aBlock) flatten.

: In its turn, the #flatten method creates a flat collection from a multiple-
: level one. That is, #(1 #(2 3) #(4 5 6) 7) turns into #(1 2 3 4 5 6 7).

In my opinion, #flatten is a perfect name and exactly the way similar
operations are usually called in Lisp community (another option is
#fringe). In the spirit of some VW conventions, though, a better variant
is #flattened--because you don't "flatten" the receiver itself, you answer
a "flattened" version of it.  In this spirit, Rectangle's #translateBy:
and #expandBy: were transformed into #translatedBy: and #expandedBy:
during The Great Objectworks 4.0->4.1 Transition.

--Vassili



Wed, 04 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:

> : ..looking for nice, short and meaningful names..
> : Imagine that you have a collection of cars and you need to get a collection
> : of all their wheels. I can write the following:
> :     ^cars flatCollect: [:e | e wheels]

> : Where Collection>>flatCollect: aBlock is
> :     ^(self collect: aBlock) flatten.

I think you mean
        ^(self flatten) collect: aBlock

Quote:
> : In its turn, the #flatten method creates a flat collection from a multiple-
> : level one. That is, #(1 #(2 3) #(4 5 6) 7) turns into #(1 2 3 4 5 6 7).

think carefully ... see below ..

Quote:

> In my opinion, #flatten is a perfect name and exactly the way similar
> operations are usually called in Lisp community (another option is
> #fringe). In the spirit of some VW conventions, though, a better variant
> is #flattened--because you don't "flatten" the receiver itself, you answer
> a "flattened" version of it.

Good grammar!
Though it might be more consistent to put this new method in the "conversions"
category of Collection instance methods and call it "asFlat".

x asFlat ---> a flattened version of x (same class as x)
eg, for arrays,
#(1 #(2 3) #(4 5 6) 7) asFlat ---> #(1 2 3 4 5 6 7)
or, for sets,
{5 12 {'hello' 27 12} 13 } asFlat ---> {5 12 'hello' 27 13}

Quote:
> : Where Collection>>flatCollect: aBlock is
> :     ^(self collect: aBlock) flatten.

I suggest not bothering to write this method.  Otherwise you should also
consider adding flatDo: flatInject: flatSelect: etc.  (all enumerating
methods).  And if you ever think of a new enumeration method, you've got
to add it in regular and flat versions all over the collection hierarchy.
Then you have to put in flatSet versions of all these things, since
quite often it will be sufficient (and much simpler) to process the
entire *set* of "leaf" items rather than a re-constructed array/bag/list
or whatever.

No, leave the choice totally de-coupled.  Retrieving the flat version of
a collection should be left as a separate job from iterating over a
collection.  Your client programs can make their own choice without a great
deal of extra keyboard strokes:

"assume flatness"
Names := People collect: [ :p | p name ]

"avoid non-flatness"
Names  := (People select: [:p|p isKindOf Person]) collect: [ :p | p name ]

"use flat version of same structure"
Names := People asFlat collect: [ :p | p name ]

"use flat set"
Names := People asSet asFlat collect: [ :p | p name ]

A benefit of this approach (apart from the decoupling) is that you
don't get in a {*filter*} mess trying to work out sensible behaviour for
flatCollect: when it hits duplicates, and you won't have hassle of
overriding it all over the place.  The "asFlat" method can probably
be written as Collection level and overridden in very few other places.
Clients control behaviour of duplicates by choosing whether to use
"asSet" or not before using "asFlat".  (or any other collection
conversion).



Fri, 06 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:

>In my opinion, #flatten is a perfect name and exactly the way similar
>operations are usually called in Lisp community (another option is
>#fringe). In the spirit of some VW conventions, though, a better variant
>is #flattened--because you don't "flatten" the receiver itself, you answer
>a "flattened" version of it.

To me, it seems better to use #flatten instead of #flattened.
In Smalltalk we send messages usually in imperative form, because we are requiring
the receivers to do something. See for example the message #do:.

  Vlastimil Adamovsky

       The contents of this message express only the sender's opinion.
       This message does not necessarily reflect the policy or views of
       my employer, Merck & Co., Inc.  All responsibility for the statements
       made in this Usenet posting resides solely and completely with the
       sender.



Fri, 06 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:


>> : ..looking for nice, short and meaningful names..
>> : Imagine that you have a collection of cars and you need to get a collection
>> : of all their wheels. I can write the following:
>> :     ^cars flatCollect: [:e | e wheels]

>> : Where Collection>>flatCollect: aBlock is
>> :     ^(self collect: aBlock) flatten.
>I think you mean
>        ^(self flatten) collect: aBlock
>> : In its turn, the #flatten method creates a flat collection from a multiple-
>> : level one. That is, #(1 #(2 3) #(4 5 6) 7) turns into #(1 2 3 4 5 6 7).
>think carefully ... see below ..

>> In my opinion, #flatten is a perfect name and exactly the way similar
>> operations are usually called in Lisp community (another option is
>> #fringe). In the spirit of some VW conventions, though, a better variant
>> is #flattened--because you don't "flatten" the receiver itself, you answer
>> a "flattened" version of it.
>Good grammar!
>Though it might be more consistent to put this new method in the "conversions"
>category of Collection instance methods and call it "asFlat".

I prefer #flattened over #asFlat since the latter implies the existence
of some class named Flat, which is clearly not the case here.
This method should probably be coded in Collection to refer to the receiver's
species and using something like #addAll:; subclasses such as Array
would reimplement the method appropriately (e.g., by collecting an
OrderedCollection and then converting it into an Array).

 2
G

--

http://anarch.ie.utoronto.ca/people/golovch/
(Mechanical and) Industrial Engineering
University of Toronto



Fri, 06 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

How about #asFlattened. This implies an active tranformation of the
collection. Also, it does not imply ( as much as #asFlat) that there is a
Flat class. Then, collections which are already flat can return themselves
and composite collections can fold themselves as a copy (as inconsistently
as the current #asWhatever methods)



Fri, 06 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Reconsider whether you should really have collections of collections.
It is often an indication that there are candidates for an object
within the structure, if any.

--

 there  _,\=\=\-\_   _,\=\=\-\_      Morgan Stanley Japan, Ltd
  yet? (*)/===/'(*) (*)/===/'(*)            Tokyo, Japan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:
> Reconsider whether you should really have collections of collections.
> It is often an indication that there are candidates for an object
> within the structure, if any.

True. But it's already implemented this way. The object in question
is called AxisGroup, and half of its functionality is managing the
axes. For example, Axis>>validate returns a collection of errors
in its numerical parameters, so I need the #flatten method for this:

AxisGroup>>validate
    ^self axes flatCollect: [:a | a validate]

Simon

 _________                Simon (Vsevolod ILyushchenko)
|       x |

|_________|   (For all normal mail)

Disclaimer: This is not me. This is just my mailer talking to your mailer...



Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name


Quote:


>: Dear All,

>: I am looking for nice, short and meaningful names for a couple of new
methods
>: in Collection. This is what they do:

>: Imagine that you have a collection of cars and you need to get a collection
>: of all their wheels. I can write the following:
>:     ^cars flatCollect: [:e | e wheels]

>: Where Collection>>flatCollect: aBlock is
>:     ^(self collect: aBlock) flatten.

>: In its turn, the #flatten method creates a flat collection from a multiple-
>: level one. That is, #(1 #(2 3) #(4 5 6) 7) turns into #(1 2 3 4 5 6 7).

>In my opinion, #flatten is a perfect name and exactly the way similar
>operations are usually called in Lisp community (another option is
>#fringe). In the spirit of some VW conventions, though, a better variant
>is #flattened--because you don't "flatten" the receiver itself, you answer
>a "flattened" version of it.  In this spirit, Rectangle's #translateBy:
>and #expandBy: were transformed into #translatedBy: and #expandedBy:
>during The Great Objectworks 4.0->4.1 Transition.

>--Vassili

would asFlattened, or asFlattenedCollection be more correctfor the one method?
perhaps the flatCollect: method could be something more simple - getAll: seems
mor intuitive & less redundant...


Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:

>To me, it seems better to use #flatten instead of #flattened.
>In Smalltalk we send messages usually in imperative form, because we
>are requiring  the receivers to do something. See for example the
>message #do:

There is a convention in VW of using the past participle if the return is
a novel object, and the imperative when the receiver is modified.

aMatrix inverse         returns the inverse without affecting aMatrix
aMatrix invert          aMatrix is now the inverse of what it was.

The returned object in the latter case is at the users discretion,
though it would make sense if it were the same as the former.
If you insist on imperative form, it is much harder to make the name
indicate whether the receiver will be modified.

IMHO this is a very useful stylistic convention, and one which I use
even when programming in other languages.

... John Woods
--
"
Forsan et haec olim meminisse iuvabit (Virgil)
[approx] Tr: The time may come when we will look back on these days and laugh
"



Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:
> would asFlattened, or asFlattenedCollection be more correctfor the one method?
> perhaps the flatCollect: method could be something more simple - getAll: seems
> mor intuitive & less redundant...

The first one does not sound good to me: we do not say #asOrdered or
#asSorted. The second may imply that there is a class FlattenedCollection.
So far I liked #flattened most of all.

As for #getAll:, it is probably too general a name. Maybe #visitAll: ?

Simon

 _________                Simon (Vsevolod ILyushchenko)
|       x |

|_________|   (For all normal mail)

Disclaimer: This is not me. This is just my mailer talking to your mailer...



Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:

>aMatrix inverse             returns the inverse without affecting aMatrix
>aMatrix invert              aMatrix is now the inverse of what it was.

Naturally, I meant `inverted'!
--
"
Forsan et haec olim meminisse iuvabit (Virgil)
[approx] Tr: The time may come when we will look back on these days and laugh
"


Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:
> I can write the following:
>     ^cars flatCollect: [:e | e wheels]

Aren't we overload the term flatten?  After executing the above,
you would probably need four spare tires :-)

-- Mike Klein



Sat, 07 Nov 1998 03:00:00 GMT  
 Collection>>flatten - looking for a better name

Quote:
> > I can write the following:
> >     ^cars flatCollect: [:e | e wheels]

> Aren't we overload the term flatten?  After executing the above,
> you would probably need four spare tires :-)

> -- Mike Klein

:)))
See what I mean by being a non-native English speaker? This thought never
crossed my mind when I was writing the example!

Simon

 _________                Simon (Vsevolod ILyushchenko)
|       x |

|_________|   (For all normal mail)

Disclaimer: This is not me. This is just my mailer talking to your mailer...



Sun, 08 Nov 1998 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Collection>>flatten

2. Collection>>flatten...

3. HELP>>>>>>>Fortran-Pascal Linking

4. >>>>>>>FROM SMTK TO C++

5. ><><><><>Heeeeeeeeeeeeeeelp on INT 14!><><><><><>

6. <<<<<YOU MUST CHECK THIS OUT >>>>>>>>>> 2103

7. >>>>>AVL tree

8. >>>>>How to read a IMAGE file?(BMP, GIF)|||||||||

9. ANNOUNCE>>>>>TOTAL FrameWork from Cincom

10. >>>ARGHH>>>got 2-100,one line email names,which way to produce a 3rd list of Uniq, diff or awk-sed

11. Slow/No Speed>>>>>>>>>

12. <<Comparing collections>>

 

 
Powered by phpBB® Forum Software