Skip to content

Instantly share code, notes, and snippets.

@gabro
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabro/1a1c1f49951b12209165 to your computer and use it in GitHub Desktop.
Save gabro/1a1c1f49951b12209165 to your computer and use it in GitHub Desktop.
Vanilla Scala exercise
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