is there a better way to achieve this? I have a l...
# getting-started
l
is there a better way to achieve this? I have a list and I want to compose it into 4 different lists
Copy code
with(screens) {
            filter { it.showProgress }.mapTo(hasProgressScreens) { it.viewId }
            filter { it.showSkip }.mapTo(hasSkipScreens) { it.viewId }
            filter { it.showToolbar }.mapTo(hasToolbar) { it.viewId }
            filter { it.showBack }.mapTo(hasBackButton) { it.viewId }
        }
d
groupby?
🤔
l
not sure if I’m getting the groupBy syntax right
.groupBy({it.showProgress} + ???)
d
would have to give it an enum or something instead
a
but groupBy would produce lists that do not intersect, right? and using four filters would produce lists that may intersect
not sure which one you want
l
hmm the list may intersect, that’s right
d
ah, then i think you have to do what you're currently doing
d
You could avoid the intermediary lists with something like this:
Copy code
mapNotNullTo(hasProgressScreens) { it.takeIf { it.showProgress }?.viewId }
l
I know this case the performance doesn’t matter too much because it has only 30 elements, but is there a way to achieve this with a sequence? Similar to doing with a loop?
now out of just curiosity
something like
Copy code
screens.forEach {  screen ->
           if(screen.showProgress) hasProgressScreens.add(screen.viewId)
           if(screen.showSkip) hasSkipScreens.add(screen.viewId)
           if(screen.showToolbar) hasToolbar.add(screen.viewId)
           if(screen.showBack) hasBackButton.add(screen.viewId)
        }
but in a sequence/functional way
h
If you create a wrapper class for the lists you can always do it as a reduce operation, not sure if it's more readable though