Last active
August 29, 2015 14:14
-
-
Save gabro/1a1c1f49951b12209165 to your computer and use it in GitHub Desktop.
Vanilla Scala exercise
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 scala.concurrent._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
def getInteger(x: Int) = Future.successful( | |
if (x == 11) None else Some(x)) | |
def sequence[T](l: Seq[Option[T]]): Option[List[T]] = l.foldRight(Some(Nil) : Option[List[T]]) { | |
(or, ox) => for (r <- or; x <- ox) yield (r :: x) | |
} | |
def computeList(n: Int): Future[Option[List[Int]]] = | |
Future.sequence((1 to n).map(getInteger)).map(sequence) | |
computeList(3) // will result in List(1, 2, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment