Flatten only the second item of a tuple

Hello all!

How do I get from this channel output

[id1, [file1]]
[id2, [file2, file3, file4]]
[id3, [file5]]

to this one

[id1, file1]
[id2, file2]
[id2, file3]
[id2, file4]
[id3, file5]

?

I have been trying with flatMap with no success…

Hello @lacb,

See the snippet below for a solution.

Channel                                                                                                                                                                                        
  .of(['id1', ['file1']], ['id2', ['file2', 'file3', 'file4']], ['id3', ['file5']])
  .flatMap { id, files ->  
    files.collect { file -> [id, file] }
  }
  .view()

Output:

1 Like

While Marcel is correct, a slightly shorter way is:

Channel
  .of(['id1', ['file1']], ['id2', ['file2', 'file3', 'file4']], ['id3', ['file5']])
  .transpose()
  .view()

although transpose may be deprecated in future for the flatMap solution above.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.