Scala深入浅出进阶经典 第89讲:Scala中使用For表达式实现内幕思考

来源:互联网 发布:初级英语听力软件 编辑:程序博客网 时间:2024/05/22 12:47
package com.dt.scalaInAction.demo_089/** * Scala中使用For表达式实现map、flatMap、filter */object For_Advanced {    def main(args: Array[String]): Unit = {}        def map[A, B](list: List[A], f: A => B): List[B] =         for(e <- list) yield f(e)        def flatMap[A, B](list: List[A], f: A => List[B]): List[B] =        for(x <- list; y <- f(x)) yield y            def filter[A](list: List[A], f: A => Boolean): List[A] =         for(e <- list; if f(e)) yield e}

map,flatMap,filter源码:


for循环下map源码:


for循环下flatMap源码:


for循环下filter源码:




0 0