Skip to content

Instantly share code, notes, and snippets.

@derekjw
Created February 7, 2012 05:45
Show Gist options
  • Save derekjw/1757491 to your computer and use it in GitHub Desktop.
Save derekjw/1757491 to your computer and use it in GitHub Desktop.
Future.filter() work-alike implemented via Filter.map() ... and works better
import akka.actor.ActorSystem
import akka.dispatch._
import akka.util.Duration
object MapFilter extends App {
implicit val system = ActorSystem()
val f = Future.traverse(List(Future(1 + 2), Future(11 + 2), Future(3 + 5))) {
_ map {
case x if x<10 => Some(x)
case _ => None
}
} map { _.flatten }
val result = Await.result(f, Duration.Inf)
println("Result = " + result)
system.shutdown
}
@mslinn
Copy link

mslinn commented Feb 7, 2012

Derek,

I tried writing a method that would incorporate the gist of your gist (pardon the pun). The method would accept a predicate (like x<10) and a future. The idea would be to call the method repeatedly so it would filter a list of futures and return a new list that does not contain MatchErrors, (), etc. I failed. The problem was that I could not figure out how to pass in the future and the predicate without an ugly invocation. Care to show me how it's done?

Thanks,

Mike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment