我是FP和Scala的新手,正在阅读Scala中的Functional Programming一书.其中在第4章的练习要求我们写一个调用的函数sequence这将转换List[Option[A]]成Option[List[A]].这Option是OptionScala库提供的重新实现.这是必需的代码.
trait Option[+A] {
/* Function to convert Option[A] to Option[B] using the function passed as an argument */
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(v) => Some(f(v))
}
/* Function to get the value in `this` option object or return the default value provided. Here,
* `B >: A` denotes that the data type `B` is either a super-type …Run Code Online (Sandbox Code Playgroud) scala ×1