package com.dt.scalaInAction.demo_088
/**
* 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
}Scala深入浅出进阶经典 第88讲:Scala中使用For表达式实现map、flatMap、filter
最新推荐文章于 2023-06-08 10:53:57 发布
本文介绍Scala中如何利用For表达式实现map、flatMap和filter等常用操作。通过具体实例展示了这些操作的简洁语法及其实现方式。

758

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



