def map[B](f: A => B): Optional[B] =
fold(f andThen some, none[B]) // what is the input for `f` function. Run `f` on what?
def some[A](a: A): Optional[A] = new Optional[A] {
def fold[X](some: A => X, none: => X) = some(a)
}
f
运行的是什么?它的输入是什么?
答案 0 :(得分:1)
f将在您提供的选项上运行。
val x = Some("test")
val y = x.map(_.toUpperCase) //y is still Option[String] value is Some(TEST)
y将被该函数转换并保持为Option(y为Option [String])。 如果您认为Option只是一个项目的集合,那么调用map或折叠就更自然了。
两个好的链接:
在第一篇文章中查找部分:选项可以视为集合
http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html
第二篇文章重点讨论了折叠与地图选项的问题。
http://kwangyulseo.com/2014/05/21/scala-option-fold-vs-option-mapgetorelse/