I have the following code to try to filter down a ...
# announcements
r
I have the following code to try to filter down a sequence, but when I call
toList()
it appears to just make an empty list. Am I doing something wrong?
Copy code
someList.asSequence()
  .also { println(it.count()) } // 11
  .filter { thing -> !thing.bool }
  .also { println(it.count()) } // 11
  .take(CONST_INT)
  .map { thing ->
      thing.apply {
          thing.prop = "newVal"
      }
  }
  .also { println(it.count()) } // 11
  .toList()
  .also { println(it.count()) } // 0
a
Are you sure its still 11 right before
toList
?
j
Assuming the following definition:
Copy code
class Thing {
  val bool get() = false
  var prop = "oldVal"
}

const val CONST_INT = 11

val someList = (1..11).map { Thing() }
The snippet you shared prints:
Copy code
11
11
11
11
So there must be something wrong outside of the snippet you just shared...
r
Honestly, nothing else is happening in the actual implementation of what I just shared. It’s precisely as written here.
Could it be that my filtering doesn’t happen until
toList()
is called, so it appears to have everything up until I call it, and then filters out everything?
a
no,
count()
iterates over the whole sequence and all operations
r
Ok, thanks. I’ll try to muddle through what I did to cause this.
Ok, I fixed my problem by moving the
map
mutation outside the sequence evaluation. I’m really unclear on why that fixes it, but the
map
mutation does affect the
filter
case in my implementation. I can’t reproduce it in a sterile environment though, and I can’t figure out what property of sequence would cause it to re-evaluate everything that way