Skip to content

Instantly share code, notes, and snippets.

@cm-kazup0n
Created June 8, 2020 01:49
Show Gist options
  • Save cm-kazup0n/7f5b470ecdf6f52490913c0295eb089a to your computer and use it in GitHub Desktop.
Save cm-kazup0n/7f5b470ecdf6f52490913c0295eb089a to your computer and use it in GitHub Desktop.
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