Skip to content

Instantly share code, notes, and snippets.

@SebastianAas
Last active January 21, 2021 10:32
Show Gist options
  • Save SebastianAas/f1b40cc94c0c10e25450756599b6c9ba to your computer and use it in GitHub Desktop.
Save SebastianAas/f1b40cc94c0c10e25450756599b6c9ba to your computer and use it in GitHub Desktop.
SetF
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)
}
def withFilter(f: A => Boolean): SetF[A] = SetF(future.map(_.filter(f)))
def map[B](f: A => B): SetF[B] = SetF(future.map(option => option.map(f)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment