Add attemptOption to Alternative#4862
Open
MavenRain wants to merge 1 commit into
Open
Conversation
Implements the standard `optional` parser-combinator (Haskell's `Control.Applicative.optional`): lift a possibly-empty F[A] into an always-non-empty F[Option[A]] by combining `map(_.some)` with `pure(None)` via `combineK`. Added to the `Alternative` trait with a default implementation and to the simulacrum-style `Ops` trait so it is reachable via `import cats.syntax.alternative._`. Closes typelevel#2936. Signed-off-by: Onyeka Obi <softwareengineerasaservant@isurvivable.cv>
Contributor
|
@MavenRain , thank you for the PR! I've got a few comments:
|
satorg
reviewed
May 16, 2026
Comment on lines
+89
to
+94
| test("attemptOption") { | ||
| assert(Alternative[Option].attemptOption(Option(5)) === Some(Some(5))) | ||
| assert(Alternative[Option].attemptOption(Option.empty[Int]) === Some(None)) | ||
| assert(Alternative[List].attemptOption(List(1, 2, 3)) === List(Some(1), Some(2), Some(3), None)) | ||
| assert(Alternative[List].attemptOption(List.empty[Int]) === List(None)) | ||
| } |
Contributor
There was a problem hiding this comment.
This test doesn't seem providing any value because the following two property based tests should cover all these cases already.
johnynek
reviewed
May 16, 2026
| * }}} | ||
| */ | ||
| def attemptOption[A](fa: F[A]): F[Option[A]] = | ||
| combineK(map(fa)((a: A) => Some(a): Option[A]), pure(Option.empty[A])) |
Contributor
There was a problem hiding this comment.
nit: I'd rather add:
private val fempty: F[Option[Nothing]] = pure(Option.empty[Nothing])
def attemptOption[A](fa: F[A]): F[Option[A]] =
combineK(map(fa)((a: A) => Some(a): Option[A]), widen[Option[A]](fempty))Since widen by default is a cast (and generally 0 cost), this prevents allocating the fempty on every call.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2936.
Adds
attemptOption[A](fa: F[A]): F[Option[A]]toAlternative,implementing the standard
optionalparser-combinator (Haskell'sControl.Applicative.optional): tryfa, surface its result asSome, and combine withpure(None)so the result always succeeds atleast once.