What is a good way( read better readability) to filter a list of tuples. I'm using
tupleList.filter(_._2).map(_._1)
But this does not feel readable.
Not sure how much better but you can use collect:
tupleList.collect { case (true, x) => x }
and of course give x some meaningful name. If the first element is not a boolean you can even do something like:
tupleList.collect { case (x, y) if (cond) => y}
and give x and y meaningful names
Using the equivalent with partial functions can also help:
tupleList.filter { case (_, snd) => snd }
.map { case (fst, _) => fst }
This should improve significantly when Dotty arrives with tuple unpacking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.