规范的方法来处理Scala中的Option元素

时间:2013-04-18 08:05:14

标签: scala tags option maybe

例如,我想将函数f: (Int,Int) => Int应用于类型为Option[Int]的两个元素。我的想法类似于(a,b).zipped.map(f),但这产生了一个List,我希望得到一个新的Option [Int]作为结果。

scala> def f(a:Int,b:Int) = a*b
f: (a: Int, b: Int)Int

scala> val x = Some(42)
x: Some[Int] = Some(42)

scala> val y:Option[Int] = None
y: Option[Int] = None

scala> (x,y).zipped.map(f)//I want None as a result here
res7: Iterable[Int] = List()

如果没有明确分支,怎么办呢?

3 个答案:

答案 0 :(得分:6)

就像scala中的许多其他操作一样,这可以通过理解来完成:

def f(a:Int,b:Int) = a*b
for (x <- maybeX; y <- maybeY) yield f(x, y)

答案 1 :(得分:1)

像这类问题一样,scalaz有一些帮助:

scala> import scalaz._
import scalaz._

scala> import Scalaz._
import Scalaz._

scala> def f(a:Int,b:Int) = a*b
f: (a: Int, b: Int)Int

scala> val x = Some(42)
x: Some[Int] = Some(42)

scala> val y:Option[Int] = None
y: Option[Int] = None

scala> ^(x,y)(f)
res0: Option[Int] = None

scala> val x = 42.some
x: Option[Int] = Some(42)

scala> (x |@| y)(f)
res3: Option[Int] = None

答案 2 :(得分:0)

使用om-nom-nom的想法,我可以这样做:

scala> def f(a:Int,b:Int) = a*b
f: (a: Int, b: Int)Int

scala> def lifted(f: (Int,Int) => Int) = (a:Option[Int],b:Option[Int]) => for(x<-a;y<-b) yield f(x,y)
lifted: (f: (Int, Int) => Int)(Option[Int], Option[Int]) => Option[Int]

scala> def liftedF = lifted(f)
liftedF: (Option[Int], Option[Int]) => Option[Int]

scala> val x = Some(42)
x: Some[Int] = Some(42)

scala> val y:Option[Int] = None
y: Option[Int] = None

scala> liftedF(x,x)
res0: Option[Int] = Some(1764)

scala> liftedF(x,y)
res2: Option[Int] = None

我们甚至可以概括一下......请盖住你的眼睛:

 def lift2[A, B, C](f: (A, B) => C): (Option[A], Option[B]) => Option[C] = (a: Option[A], b: Option[B]) =>
    for (x <- a; y <- b) yield f(x, y)