Scala之高阶函数

目标

  1. sorted
  2. sortBy
  3. sortWith
  4. flatten
  5. map
  6. flatMap
  7. filter
  8. groupBy
  9. flod
  10. reduce
  11. wc

sorted

排序,默认升序

val array = Array(1, 2, 5, 4, 3)
array.sorted

sortBy

排序,指定排序规则

val arrMap = Array(('a', 1), ('b', 4), ('d', 3), ('c', 2))
arrMap.sortBy(_._1)
arrMap.sortBy(_._2)

sortWith

排序,多个字段联合排序

val arrMap2 = Array(("a", 2), ("a", 1), ("b", 2), ("d", 5), ("c", 4))
arrMap2.sortWith((t1,t2)=>{
if (!t1._1.equalsIgnoreCase(t2._1)){
t1._1.compareTo(t2._1)<0
}else{
t1._2.compareTo(t2._2)<0
}
})

flatten

扁平化

val flatten = Array(Array(1, 2, 3), Array(4, 5, 6))
flatten.flatten

map

映射

val map = Array("this is a demo,hello world")
map.map(_.split(" "))

map.map(_.split(" ")).flatten.map((_,1))

flatMap

flatMap ==> flatten + map

val flatMap = Array("this is a demo", "hello word")
flatMap.flatMap(_.split(" "))
flatMap.flatMap(_.split(" ")).map((_,1))

flatMap.flatMap(line => (for (i<- line.split(" ")) yield (i,1)))

filter

过滤

val filter = Array("this is a demo", "hello word")
filter.filter(_.contains("hello"))
filter.filterNot(_.contains("hello"))

groupBy

分组

val groupBy = Array("good", "good", "study")
groupBy.groupBy(x=>x)
groupBy.groupBy(x=>x).map(t=>(t._1,t._2.size))

flod

聚合,需要指定默认值

val flod = Array(1, 2, 3, 1, 2, 4, 5, 6, 8, 4)
flod.fold(10)((a,b)=>{
println(a+" : "+b)
a-b
})

reduce

聚合

val reduce = Array(1, 2, 3, 1, 2, 4, 5, 6, 8, 4)
reduce.reduce(_+_)
reduce.reduce(_-_)
reduce.reduceLeft(_+_)
reduce.reduceLeft(_-_)
reduce.reduceRight(_+_)
reduce.reduceRight(_-_)

wc

方案1

val wc = Array("this is demo", "good good study", "day day up")
wc.flatMap(_.split(" "))
.groupBy(word=>word)
.toList
.map(t=>(t._1,t._2.size))
.sortBy(_._1)
.mkString(",")

方案2

wc.flatMap(_.split(" "))
.map((_,1))
.groupBy(_._1)
.toList
.map(t=>(t._1,t._2.size))
.sortBy(_._1)
.mkString("\t")
Author: Tunan
Link: http://yerias.github.io/2020/03/03/scala/1/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.