相关疑难解决方法(0)

将列表[选项[A]]转换为Scala中的选项[列表[A]]

我是FP和Scala的新手,正在阅读Scala中的Functional Programming一书.其中在第4章的练习要求我们写一个调用的函数sequence这将转换List[Option[A]]Option[List[A]].这OptionOptionScala库提供的重新实现.这是必需的代码.

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

10
推荐指数
1
解决办法
3141
查看次数

标签 统计

scala ×1