Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

group-by function (See related posts)

    group-by: func [  
        {Returns a block of blocks+sub-blocks with items partitioned by
        matching index elements in each sub-block.}
        block  [any-block!] "A block of blocks"
        index  [integer!] "Index of sub-block value to compare, for grouping."
        /local keys
    ][
        result: copy []
        foreach item block [
            if not find/skip result item/:index 2 [
                append result reduce [item/:index copy []]
            ]
        ]
        foreach item block [
            append/only select result item/:index item
        ]
        result
    ]
    ;group-by [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5]] 1
    ;group-by [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5] [c 2 4]] 2
    ;group-by [[a 1 2] [b 2 3] [a 2 4] [c 2 3] [b 1 5] [c 2 4]] 3

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts