Created
June 8, 2020 01:49
-
-
Save cm-kazup0n/7f5b470ecdf6f52490913c0295eb089a to your computer and use it in GitHub Desktop.
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
package util | |
import scala.concurrent.{ExecutionContext, Future} | |
/** | |
* Future[Option[A]]のラッパー | |
* @param value | |
* @tparam A | |
*/ | |
final case class OptionF[A](value: Future[Option[A]]) { | |
def map[B](f: A => B)(implicit ec: ExecutionContext): OptionF[B] = OptionF(value.map(_.map(f))) | |
def semiFlatMap[B](f: A => Future[B])(implicit ec: ExecutionContext): OptionF[B] = OptionF(value.flatMap(o => o.fold[Future[Option[B]]](Future.successful(None))(a => f(a).map(b => Some(b))))) | |
def fold[B](empty: B)(f: A => B)(implicit ec: ExecutionContext): Future[B] = value.map(_.fold(empty)(f)) | |
} | |
object OptionF { | |
def fromOption[A](a: Option[A]): OptionF[A] = OptionF(Future.successful(a)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment