sudom82 sudom82 - 27 days ago 5x
Scala Question

Flatten syntax with yield - improving code readability

I'm trying to improve the readability of my code and I'm having a hard time with this little chunk.

Foo is a method that accepts a List[Ping]

Thing.generate returns a List[Ping]

ListOfPings is a List[Ping]

hasQuality returns a boolean value from evaluating a Ping

Here's the code:

foo((for {
pinger <- listOfPings
} yield pinger.generate.filter(_.hasQuality)).flatten)

Each Ping in listOfPingss is creating a List[Thing] with the generate method, meaning the result of the yield at the end of the loop is a List[List[Ping]].

I'm flattening that List[List[Ping]] (not the individual lists), and putting the whole result into foo

I'm having trouble making this look nicer, potentially with a flatmap? I sincerely appreciate the help.


Something like:

foo {
  for (p <- listOfPings ; q <- p.generate if q.hasQuality) yield q