pharo

Retrieve an element of an orderedcollection in pharo


I have a difficult problem. I have a method that return an OrderedCollection(an orderedCollection(String),an orderedCollection(String), an orderedCollection(Date)).

I want to have this result an orderedCollection(String, String, Date). How Can i do ?


Solution

  • If your composite collection only includes subcollections with one element, you can use

    result := theCollection collect: [:sc| sc first]
    

    where theCollection stands for your OrderedCollection of OrderedCollections.

    Otherwise, if the subcollections do not meet this condition and you want to put all elements into a single collection use

    result := theCollection gather: [:sc | sc]
    

    For example:

    #(#(1) #(2) #(3)) collect: [:sc | sc first] 
    

    will give as result the Array #(1 2 3) (similarly for OrderedCollections).

    On the other hand

    #(#(1 2) #(3) #(4 5 6)) gather: [:sc | sc] 
    

    will produce #(1 2 3 4 5 6).