Is there a way to flatten the amount of calls to f...
# getting-started
c
Is there a way to flatten the amount of calls to flatMap?
Copy code
myScreenState.listOf20Items.flatMap { it.subListOf10Items }.flatMap { it.anotherSubListOf10Items }
        .filter { it.isDone }.size
i was initially thinking maybe I could do
Copy code
myScreenState.listOf20Items.flatMap { it.subListOf10Items.anotherSubListOf10Items }
        .filter { it.isDone }.size
a
are you concerned about the performance impact of chaining the flatMaps? Personally I wouldn’t worry about it, because it’s usually not going to be that bad. If you wanted to avoid problems with chained flatMaps it you could use a Sequence. This can help with performance, but it can also hinder it There’s also
count {}
, which you can use instead of
filter {}.size
I think this is nice and readable:
Copy code
myScreenState
  .listOf20Items
  .asSequence()
  .flatMap { it.subListOf10Items }
  .flatMap { it.anotherSubListOf10Items }
  .count { it.isDone }
c
not concerned about perf. just that two flat maps in a row seems "weird" and didn't know if there was a shorthand to chained flatmaps.