映射,过滤器,减少 - 功能数据管道设计的高阶函数

深度潜水在高阶函数以及它们如何用于设计功能数据流水线的数据转换

Recap

数据处理和数据流水线设计模式

Data Pipeline Design Pattern (Image by author)

数据变换的高阶函数

ETL进程的剪影(由作者的图像)

地图

素描地图function (Image by author)

筛选

素描筛选function (Image by author)
def filter(p:(a)=> boolean):用于集合类的erreable [a] //
def filter(p: (A) => Boolean): Iterator[A] // for iterators that access elements of a collection
def isEven(x):
返回x%2 == 0
def main(args):
collection = [1,2,3,4,5]
evenNums = list(filter(isEven, collection))
打印(赤裸)
object FilterEven {def isEven(x: Int): Int = {
x%2 == 0
}
def main(args:array [string]){
Val Collection = list [1,2,3,4,5]
val alennums = collection.filter(ISeven)
println(erynums)
}
}

减少

素描减少function (Image by author)
DEF减少(b>:a)(op:(b,b)=> b):b
def reduceLeft[B >: A](op: (B, A) => B): B
def reduceRight[B >: A](op: (A, B) => B): B
def main(args):
从Functools进口减少
集合= [1,3,5,2,4]
totalSum = reduce(lambda x,y: x + y, collection)
print(totalSum)
对象Sumnumbers {def main(args:array [string]){
val集合=列表[1,3,5,2,4]
val totalsum = collection.reduce((x,y)=> x + y)
println(totalSum)
}
}
[1,3,5,2,4].reduceLeft((x, y) => x + y) // initialize var acc = null
(((1 + 3)+ 5)+ 2)+ 4 //取得第一值,ACC = 1
((4 + 5) + 2) + 4 // acc = 1 + 3 = 5
(9 + 2) + 4 // acc = 4 + 5 = 9
11 + 4 // ACC = 9 + 2 = 11
15 // ACC = 11 + 4 = 15收集结束时返回
[1,3,5,2,4] .Reduceright((x,y)=> x + y)// initialize var Acc = null
1 +(3 +(5 +(2 + 4)))//从右边取第一值,ACC = 4
1 +(3 +(5 + 6))// ACC = 4 + 2 = 6
1 + (3 + 11))// acc = 6 + 5 = 11
1 + 14 // acc = 11 + 3 = 14
15 // acc = 14 + 1 = 15 returned upon end of collection

把它们整合在一起

下一步是什么

在夜间用DT的尺度建立数据管道,在晚上逐个,技术演讲者和作家。90%的自学式开发商和终身工程师。-https://ongchinhwee.me

Baidu
map