Skip to content
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 option.diff #178

Open
OndrejSpanel opened this issue Apr 19, 2024 · 1 comment
Open

Add option.diff #178

OndrejSpanel opened this issue Apr 19, 2024 · 1 comment

Comments

@OndrejSpanel
Copy link
Member

OndrejSpanel commented Apr 19, 2024

Add diff to Option for a symmetry with Seq. Reference implementation:

  implicit class OptionValueOps[T](private val o: Option[T]) extends AnyVal {
    final def diff(that: Option[T]): Option[T] = {
      if (that.exists(o.contains)) None
      else o
    }
  }
@OndrejSpanel
Copy link
Member Author

OndrejSpanel commented May 22, 2024

It can be even made a little bit more general:

implicit class OptionValueOps[T](private val o: Option[T]) extends AnyVal {
  final def diff(that: Iterable[T]): Option[T] = {
    if (o.exists(that.contains)) None
    else o
  }
}

Note: o.exists(that.contains) should be more performant than that.exists(o.contains) when that.contains is more efficient than that.exists, e.g. for Set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant