RxSwift函数式响应编程
函数式编程
1.数学 - 函数式
y = f(x)
y = f(f(x))
x = f(x)
// x = 变量
// f = 函数
// y = 返回值
// f(x) 是一个表达式
比如 2 = 1 + 1
或者 2 = 0 + 2
2.函数式编程
2.1 函数响应式编程(FRP) Functional Reactive Programming
/**
需求
首先获取 >3的数字
获取的数字之后 + 1
所有数字中的偶数
*/
let arr = [1,3,5,7,9]
// if else 处理
for num in array {
if num > 3 {
let number = num + 1
if (number % 2 == 0){
print(number)
}
}
}
// 函数式
// filter 过滤器
array.filter{$0 > 3}
.filter{ ($0+1) % 2 == 0 }
.forEach{print($0+1)} // 6 8
/// R --- 响应式
/// 伪代码
/// Int a = 10
/// Int b = a + 10 == 关系
/// a + 10 = 20
/// b = ? 30
/// KVO 机制