What about ```val items = ingredentsList.flatMap {...
# stdlib
k
What about
Copy code
val items = ingredentsList.flatMap { listOf(ListItem.HeaderItem(it.type), ListItem.IngredientItem(it)) }
?
HeaderItem
will need a correctly implemented `equals`/`hascode` for this.
l
hmm in this case the order of
items
collection matter actually
k
That's why I asked ☺️
l
it has to be
Copy code
Header
Item
Item
Header
Item
Item 
...
sorry only realized now 😅
k
You could do this:
Copy code
val items = listIngredients.groupBy{ it.type }.flatMap {listOf(ListItem.HeaderItem(it.key)) + it.value.map { ListItem.IngredientItem(it) } }
but that starts to get forced.
👍 1
l
what you mean by forced?
k
It's less readable then the original, and you're creating a bunch of useless lists along the way.
l
oh I see
I was looking at koans collections examples, but I couldn't find anything close to what I want, maybe
fold
but I'm not sure how to use it
i
@karelpeeters Your proposal with
flatMap
is fine, just format it nicely and it would be as readable as the original with
ArrayList
.
👍 1