-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add foldF, cataF and emptyflatTap to OptionT #3335
Changes from 7 commits
12551ca
a48ed5a
69c0f01
3dfd6bb
6fa9007
ff699db
f5d0419
e3f83ab
468e4f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,21 @@ final case class OptionT[F[_], A](value: F[Option[A]]) { | |||||
def fold[B](default: => B)(f: A => B)(implicit F: Functor[F]): F[B] = | ||||||
F.map(value)(_.fold(default)(f)) | ||||||
|
||||||
/** Transform this `OptionT[F, A]` into a `F[C]`. | ||||||
* | ||||||
* Example: | ||||||
* {{{ | ||||||
* scala> import cats.implicits._ | ||||||
* scala> import cats.data.OptionT | ||||||
* | ||||||
* scala> val optionT: OptionT[List, Int] = OptionT[List, Int](List(Some(23), None)) | ||||||
* scala> optionT.foldF(Nil)(v => List(v, v * 2)) | ||||||
* res0: List[Int] = List(23, 46) | ||||||
* }}} | ||||||
*/ | ||||||
def foldF[B](default: => F[B])(f: A => F[B])(implicit F: FlatMap[F]): F[B] = | ||||||
F.flatMap(value)(_.fold(default)(f)) | ||||||
|
||||||
/** | ||||||
* Catamorphism on the Option. This is identical to [[fold]], but it only has | ||||||
* one parameter list, which can result in better type inference in some | ||||||
|
@@ -24,6 +39,14 @@ final case class OptionT[F[_], A](value: F[Option[A]]) { | |||||
def cata[B](default: => B, f: A => B)(implicit F: Functor[F]): F[B] = | ||||||
fold(default)(f) | ||||||
|
||||||
/** | ||||||
* Effectful catamorphism on the Option. This is identical to [[foldF]], but it only has | ||||||
* one parameter list, which can result in better type inference in some | ||||||
* contexts. | ||||||
*/ | ||||||
def cataF[B](default: => F[B], f: A => F[B])(implicit F: FlatMap[F]): F[B] = | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am aware of the notions of catamorphisms, but is that also a unified term or notion across Unless we want to extend the use across the whole of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a |
||||||
foldF(default)(f) | ||||||
|
||||||
def map[B](f: A => B)(implicit F: Functor[F]): OptionT[F, B] = | ||||||
OptionT(F.map(value)(_.map(f))) | ||||||
|
||||||
|
@@ -45,6 +68,9 @@ final case class OptionT[F[_], A](value: F[Option[A]]) { | |||||
def semiflatMap[B](f: A => F[B])(implicit F: Monad[F]): OptionT[F, B] = | ||||||
flatMap(a => OptionT.liftF(f(a))) | ||||||
|
||||||
def semiflatTap[B](f: A => F[B])(implicit F: Monad[F]): OptionT[F, A] = | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have had this conversation before: given that the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
semiflatMap(a => F.as(f(a), a)) | ||||||
|
||||||
def mapFilter[B](f: A => Option[B])(implicit F: Functor[F]): OptionT[F, B] = | ||||||
subflatMap(f) | ||||||
|
||||||
|
@@ -63,6 +89,13 @@ final case class OptionT[F[_], A](value: F[Option[A]]) { | |||||
def subflatMap[B](f: A => Option[B])(implicit F: Functor[F]): OptionT[F, B] = | ||||||
transform(_.flatMap(f)) | ||||||
|
||||||
/** | ||||||
* Perform an effect if the value inside the is a `None`, leaving the value untouched. Equivalent to [[orElseF]] | ||||||
* with an effect returning `None` as argument. | ||||||
*/ | ||||||
def flatTapNone[B](f: => F[B])(implicit F: Monad[F]): OptionT[F, A] = | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
OptionT(F.flatTap(value)(_.fold(F.void(f))(_ => F.unit))) | ||||||
|
||||||
def getOrElse[B >: A](default: => B)(implicit F: Functor[F]): F[B] = | ||||||
F.map(value)(_.getOrElse(default)) | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add more informative names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree with your intention (and have hence changed it on
flatTapNone
– thx), I would value consistency withfold
more and keep the parameter names the same.