Created
February 7, 2012 05:45
-
-
Save derekjw/1757491 to your computer and use it in GitHub Desktop.
Future.filter() work-alike implemented via Filter.map() ... and works better
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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