如何在 Scala 中组合 Option 值?

发布于 2024-08-30 09:55:48 字数 495 浏览 13 评论 0原文

我希望能够应用操作 f: (T,T) => Scala 中的 TOption[T] 值。如果两个值中的任何一个为 None,我希望结果为 None

更具体地说,我想知道是否有更短的方法来执行以下操作:

def opt_apply[T](f: (T,T) => T, x: Option[T], y: Option[T]): Option[T] = {
  (x,y) match {
    case (Some(u),Some(v)) => Some(f(u,v))
    case _ => None
  }
}

我已尝试 (x zip y) map {case (u,v) =>; f(u,v)} 但结果是一个 Iterator[T] 而不是 Option[T]

I want to be able to apply an operation f: (T,T) => T to Option[T] values in Scala. I want the result to be None if any of the two values is None.

More specifically, I want to know if there is a shorter way to do the following:

def opt_apply[T](f: (T,T) => T, x: Option[T], y: Option[T]): Option[T] = {
  (x,y) match {
    case (Some(u),Some(v)) => Some(f(u,v))
    case _ => None
  }
}

I have tryied (x zip y) map {case (u,v) => f(u,v)} but the result is an Iterator[T] not an Option[T].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心舞飞扬 2024-09-06 09:55:48
scala> val (x, y) = (Some(4), Some(9))
x: Some[Int] = Some(4)
y: Some[Int] = Some(9)

scala> def f(x: Int, y: Int) = Math.max(x, y)
f: (x: Int,y: Int)Int

scala> for { x0 <- x; y0 <- y } yield f(x0, y0)
res26: Option[Int] = Some(9)

scala> val x = None
x: None.type = None

scala> for { x0 <- x; y0 <- y } yield f(x0, y0)
res27: Option[Int] = None
scala> val (x, y) = (Some(4), Some(9))
x: Some[Int] = Some(4)
y: Some[Int] = Some(9)

scala> def f(x: Int, y: Int) = Math.max(x, y)
f: (x: Int,y: Int)Int

scala> for { x0 <- x; y0 <- y } yield f(x0, y0)
res26: Option[Int] = Some(9)

scala> val x = None
x: None.type = None

scala> for { x0 <- x; y0 <- y } yield f(x0, y0)
res27: Option[Int] = None
七禾 2024-09-06 09:55:48

@RahulG 的答案利用了 Option 是一个 monad 的事实(尽管 Scala 库中没有类型来表示它)。编译器将 for 理解扩展为以下内容:

def a: Option[Int]
def b: Option[Int]
val calc: Option[Int] = a flatMap {aa => b map {bb => aa + bb}}

在 Scalaz 的帮助下,您还可以将其视为应用函子:

import scalaz._
import Scalaz._

def a: Option[Int]
def b: Option[Int]
val calc: Option[Int] = (a ⊛ b) {_ + _}

一个关键的区别是,在单子计算中,失败(即 < code>None) 的计算a 会短路评估。在应用风格中,ab 都会被求值,如果两者都是 Some,则调用纯函数。您还可以看到,在一元计算中,值aa可能已在计算b中使用;在应用版本中,b 不能依赖于a 的结果。

@RahulG's answer exploits the fact that Option is a monad (even though there is no type to represent this in the Scala library). The compiler expands the for comprehension to the following:

def a: Option[Int]
def b: Option[Int]
val calc: Option[Int] = a flatMap {aa => b map {bb => aa + bb}}

You can also treat it as an applicative functor, with some help from Scalaz:

import scalaz._
import Scalaz._

def a: Option[Int]
def b: Option[Int]
val calc: Option[Int] = (a ⊛ b) {_ + _}

A key difference is that in the monadic calculation, a failure (that is, None) of calculation a short circuits the evaluation. In the applicative style, both a and b are evaluated, and if both are Somes, the pure function is called. You can also see that in the monadic calculation, the value aa could have been used in the calculation b; in the applicative version, b cannot depend on the result of a.

小矜持 2024-09-06 09:55:48

我的 scalaz 版本比 retronym 稍旧,但以下内容对我来说是一个示例,并且可以推广到有 3 种类型 T、U 的情况、V 而不仅仅是一个:

def main(args: Array[String]) {
  import scalaz._
  import Scalaz._

  val opt1 = some(4.0) //Option[Double]
  val opt2 = some(3)   //Option[Int]

  val f: (Double, Int) => String = (d, i) => "[%d and %.2f]".format(i, d)

  val res = (opt1 <|*|> opt2).map(f.tupled)
  println(res) //Some([3 and 4.00])
}

然后我可以添加:

val opt3 = none[Int]
val res2 = (opt1 <|*|> opt3).map(f.tupled)
println(res2) //None

I have a slightly older version of scalaz than retronym but the following works for me as an example and is generalizable for the case where you have 3 types T, U, V and not just one:

def main(args: Array[String]) {
  import scalaz._
  import Scalaz._

  val opt1 = some(4.0) //Option[Double]
  val opt2 = some(3)   //Option[Int]

  val f: (Double, Int) => String = (d, i) => "[%d and %.2f]".format(i, d)

  val res = (opt1 <|*|> opt2).map(f.tupled)
  println(res) //Some([3 and 4.00])
}

I can then add:

val opt3 = none[Int]
val res2 = (opt1 <|*|> opt3).map(f.tupled)
println(res2) //None
南街女流氓 2024-09-06 09:55:48

从 Scala 2.13 开始,Option#zip 可以应用于另一个 Option 返回一个 Option(在早期版本中,它会返回一个 Iterable);因此:

def optApply[T](f: (T,T) => T, a: Option[T], b: Option[T]): Option[T] =
  a.zip(b).map(f.tupled)

zip 的行为是:

Some(2).zip(Some(3)) // Some((2, 3))
Some(2).zip(None)    // None
None.zip(Some(3))    // None
None.zip(None)       // None

并且可以这样应用:

optApply[Int]((a, b) => a max b, Some(2), Some(5)) // Some(5)
optApply[Int]((a, b) => a max b, Some(2), None)    // None

Starting Scala 2.13, Option#zip can be applied to another Option to return an Option (in earlier versions, it would have returned an Iterable); thus:

def optApply[T](f: (T,T) => T, a: Option[T], b: Option[T]): Option[T] =
  a.zip(b).map(f.tupled)

where the behavior of zip is:

Some(2).zip(Some(3)) // Some((2, 3))
Some(2).zip(None)    // None
None.zip(Some(3))    // None
None.zip(None)       // None

and which can be applied as such:

optApply[Int]((a, b) => a max b, Some(2), Some(5)) // Some(5)
optApply[Int]((a, b) => a max b, Some(2), None)    // None
叹倦 2024-09-06 09:55:48

您可以用于理解:

def opt_apply[T](f: (T,T) => T, x: Option[T], y: Option[T]): Option[T] = 
     for (xp <- x; yp <- y) yield (f(xp,yp))

Which is Sugar for:

x flatMap {xp => y map {yp => f(xp, yp)}}

这也是可能的,因为 Option 是 Monad

You can use for comprehensions:

def opt_apply[T](f: (T,T) => T, x: Option[T], y: Option[T]): Option[T] = 
     for (xp <- x; yp <- y) yield (f(xp,yp))

Which is sugar for:

x flatMap {xp => y map {yp => f(xp, yp)}}

This is also possible due to Option being a Monad

萝莉病 2024-09-06 09:55:48
def optApply[A,B,C](f: (A, B) => C, a: Option[A], b: Option[B]): Option[C] =
  a.zip(b).headOption.map { tup => f.tupled(tup) }

a.zip(b) 确实会产生一个 Iterable[(A, B)] (因为它来自 Options,所以最多有一个元素)。然后 headOption 返回第一个元素作为选项。

def optApply[A,B,C](f: (A, B) => C, a: Option[A], b: Option[B]): Option[C] =
  a.zip(b).headOption.map { tup => f.tupled(tup) }

a.zip(b) does result in an Iterable[(A, B)] (with, because it's from Options, at most one element). headOption then returns the first element as an Option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文