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
def example2() = { | |
val a = Future(0.5) | |
val b = Future(Set(1, 2, 3)) | |
for { | |
x <- SetF.lift(a) | |
y <- SetF(b) | |
} yield { | |
x * y | |
} | |
} |
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
object SetF { | |
def lift[A](f: Future[A]): SetF[A] = SetF(f.map(Set(_))) | |
def apply[A](o: Iterable[A]): SetF[A] = | |
SetF(Future.successful(o match { | |
case o: Set[_] => o.asInstanceOf[Set[A]] | |
case o => o.toSet | |
})) | |
} |
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
def addAllCombinations() = { | |
val a = Future(Set(1, 2, 3)) | |
val b = Future(Set(1, 2, 3)) | |
for { | |
x <- SetF(a) | |
y <- SetF(b) if y > 2 | |
} yield { | |
x + y | |
} | |
} // Result: Future[Set(4,5,6)] |
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
final case class SetF[A](_future: Future[Iterable[A]]) extends AnyVal { | |
def future: Future[Set[A]] = _future.map(_.toSet) | |
def flatMap[B](f: A => SetF[B]): SetF[B] = { | |
val newFuture: Future[Set[B]] = for { | |
list: Set[A] <- future | |
result: Set[Set[B]] <- Future.sequence(list.map(f(_).future)) | |
} yield { | |
result.flatten | |
} | |
SetF(newFuture) |
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
def example() = { | |
val a1 = Set(1, 2, 3) | |
val b2 = Set(1, 2, 3) | |
a1 | |
.flatMap(x => | |
b2 | |
.withFilter(y => y > 2) | |
.map(y => x + y) | |
) | |
} |
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
def example() = { | |
val a1 = Set(1, 2, 3) | |
val b2 = Set(1, 2, 3) | |
for { | |
x <- a1 | |
y <- b2 if y > 2 | |
} yield { | |
x + y | |
} | |
} |
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
for { | |
a1 <- Future(Set(1, 2, 3)) | |
b1 <- Future(Set(1, 2, 3)) | |
} yield { | |
for { | |
x <- a1 | |
y <- b1 if y > 2 | |
} yield { | |
x + y | |
} |