listgroovyflattenpurely-functional

Flatten list of lists by one Level in Groovy without mutating variables


Imagine a list of lists, for example

List lists = [[[1]], [[2]], [[3]], [[4]], [[5]], [[6]], [[7]], [[8]], [[9]], [[10]]]

This is just an example - the inner lists may contain multiple elements. I want to flatten the list by one level without mutating variables. The result should be

[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]

Is there a good way in Groovy for that? It should be almost as fast as

lists.inject([]) { a, b -> a.addAll(b); a }

Solution

  • Ah, apparently that is what lists.collectMany { it } is for...

    collectMany() is a bit like flatMap in Scala or Java Streams. It does not mutate variables at top level and is reasonably fast: Not as fast as

    lists.inject([]) { a, b -> a.addAll(b); a }
    

    but much faster than

    lists.stream().flatMap(List::stream).toList() 
    

    I have played around with it and collectMany() is more powerful than the standard flatMap and pretty cool to use, also for concatenating Maps.