this blog曾经有一个有用的Scala Option备忘单,但它似乎已被删除。有没有人保存过它?
答案 0 :(得分:3)
注意:senia has provided the whole original post,其中包括介绍和备忘单(在此答案中也如下)。
This blog did!(我没有关联)。有用的备忘单:
flatMap:
// This code is equivalent to:
// option.flatMap(foo(_))
option match {
case None => None
case Some(x) => foo(x)
}
弄平:
// This code is equivalent to:
// option.flatten
option match {
case None => None
case Some(x) => x
}
图:
// This code is equivalent to:
// option.map(foo(_))
option match {
case None => None
case Some(x) => Some(foo(x))
}
的foreach:
// This code is equivalent to:
// option.foreach(foo(_))
option match {
case None => {}
case Some(x) => foo(x)
}
isDefined:
// This code is equivalent to:
// option.isDefined
option match {
case None => false
case Some(_) => true
}
的isEmpty:
// This code is equivalent to:
// option.isEmpty
option match {
case None => true
case Some(_) => false
}
的forall:
// This code is equivalent to:
// option.forall(foo(_))
option match {
case None => true
case Some(x) => foo(x)
}
存在:
// This code is equivalent to:
// option.exists(foo(_))
option match {
case None => false
case Some(x) => foo(x)
}
否则容易:
// This code is equivalent to:
// option.OrElse(foo)
option match {
case None => foo
case Some(x) => Some(x)
}
getOrElse:
// This code is equivalent to:
// option.getOrElse(foo)
option match {
case None => foo
case Some(x) => x
}
toList:
// This code is equivalent to:
// option.toList
option match {
case None => Nil
case Some(x) => x :: Nil
}
coflatMap *:
// This code is equivalent to:
// option.coflatMap(foo(_))
option match {
case None => None
case Some(_) => Some(foo(option))
}
重复*:
// This code is equivalent to:
// option.duplicate
option match {
case None => None
case Some(_) => Some(option)
}
*:不幸的是,coflatMap和duplicate不是标准库的一部分。您需要自己编写或使用Scalaz。
摘录结束
答案 1 :(得分:2)
您可以在archive.org上获得完整版(带介绍)。