Last active
January 21, 2021 10:32
-
-
Save SebastianAas/f1b40cc94c0c10e25450756599b6c9ba to your computer and use it in GitHub Desktop.
SetF
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) | |
} | |
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