FlatMap

object FlatMap {

  import FoldRight._
  import AppendByFoldLeft._

  def flatMap[A, B](as: List[A])(f: A => List[B]): List[B] = foldRight(as, Nil: List[B])((a: A, b: List[B]) => append(f(a), b))

  def main(args: Array[String]): Unit = {
    println(flatMap(List(1, 2, 3))(i => List(i, i)))
  }

}
List(1, 1, 2, 2, 3, 3)
时间: 2024-09-30 15:28:00

FlatMap的相关文章

spark 教程三 spark Map filter flatMap union distinct intersection操作

RDD的创建 spark 所有的操作都围绕着弹性分布式数据集(RDD)进行,这是一个有容错机制的并可以被并行操作的元素集合,具有只读.分区.容错.高效.无需物化.可以缓存.RDD依赖等特征 RDD的创建基础RDD 1.并行集合(Parallelized Collections):接收一个已经存在的Scala集合,然后进行各种并行运算 var sc=new SparkContext(conf) var rdd=sc.parallelize(Array(2,4,9,3,5,7,8,1,6)); rd

SparkContext, map, flatMap, zip以及例程wordcount

SparkContext 通常作为入口函数,可以创建并返回一个RDD. 如把Spark集群当作服务端那Spark Driver就是客户端,SparkContext则是客户端的核心: 如注释所说 SparkContext用于连接Spark集群.创建RDD.累加器(accumlator).广播变量(broadcast variables) map操作: 会对每一条输入进行指定的操作,然后为每一条输入返回一个对象: flatMap操作: "先映射后扁平化" 操作1:同map函数一样:对每一条

Spark 中 map 与 flatMap 的区别

通过一个实验来看Spark 中 map 与 flatMap 的区别. 步骤一:将测试数据放到hdfs上面 hadoopdfs -put data1/test1.txt /tmp/test1.txt 该测试数据有两行文本: 步骤二:在Spark中创建一个RDD来读取hdfs文件/tmp/test1.txt 步骤三:查看map函数的返回值 得到map函数返回的RDD: 查看map函数的返回值--文件中的每一行数据返回了一个数组对象 步骤四:查看flatMap函数的返回值 得到flatMap函数返回的

scala flatMap reduceLeft foldLeft

object collection_t1 { def flatMap1(): Unit = { val li = List(1,2,3) val res = li.flatMap(x => x match { case 3 => List('a','b') case _ => List(x*2) }) println(res) } def map1(): Unit = { val li = List(1,2,3) val res = li.map(x => x match { ca

spark 的一些常用函数 filter,map,flatMap,lookup ,reduce,groupByKey

定义不带参数也不带返回值的函数(def :定义函数的关键字  printz:方法名称) scala> def printz = print("scala hello") 定义带参数也带返回值的函数(这种函数在定义时也可以不带返回值的类型,scala会自动推算出.建议还是带上) scala> def minNum(x:Int,y:Int):Int = if(x>y) x else y //:Int 是该函数的返回值类型 minNum: (x: Int, y: Int)I

Spark2 DataSet 创建新行之flatMap

val dfList = List(("Hadoop", "Java,SQL,Hive,HBase,MySQL"), ("Spark", "Scala,SQL,DataSet,MLlib,GraphX")) dfList: List[(String, String)] = List((Hadoop,Java,SQL,Hive,HBase,MySQL), (Spark,Scala,SQL,DataSet,MLlib,GraphX

RxJava1.0 flatMap方法的源码分析

RxJava1.0 flatMap方法的源码分析 package com.yue.test; import java.awt.Cursor; import java.util.ArrayList; import java.util.List; import com.yue.bean.Course; import com.yue.bean.Student; import rx.Observable; import rx.Subscription; import rx.Observable.OnSu

[Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight

Learn a few advanced reduction patterns: flatten allows you to merge a set of arrays into a single array, the dreaded flatmap allows you to convert an array of objects into an array of arrays which then get flattened, and reduceRight allows you to in

Scala 深入浅出实战经典 第88讲:Scala中使用For表达式实现map、flatMap、filter

高级函数 map,flatMap,filter用for循环的实现. package com.dt.scala.forexpression object For_Advanced { def main(args: Array[String]) {} def map[A, B](list: List[A], f: A => B): List[B] = for(element <- list) yield f(element) def flatMap[A, B](list: List[A], f:

[RxJS] map vs flatMap

What's the difference between map and flatmap? First, let's show what map is. To show that, I need a source stream, so I'm going to make an interval. It takes a tenth of a second, and I'm only going to take 10 values, and subscribe to it. var source