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源码:
本文介绍了Scala中如何使用For表达式实现map、flatMap和filter操作,并提供了具体的代码示例。

714

被折叠的 条评论
为什么被折叠?



