lapinkoira
lapinkoira

Reputation: 8988

Apply List.flatten to a Stream

The following pipe:

items
...
|> Stream.map(&process/1)

Generates this kind of structure:

[ [], [], [], [] ]

and I would like it to be a flatten list.

Without using streams I would just do:

|> Enum.map(&process/1)
|> List.flatten

But I would like to use it as a stream but cant figure out how to apply List.flatten and generate a Stream.

Upvotes: 0

Views: 684

Answers (1)

Paweł Dawczak
Paweł Dawczak

Reputation: 9639

You could try using Stream.flat_map/2, something this should help:

items
...
|> Stream.flat_map(&process/1)

This will keep processing elements in your items, and flatten the results.

Hope that helps!

Upvotes: 2

Related Questions