74. 实现 Observable Transformation Operators

容易  -通过 / -执行

本问题是 57. 实现Observable的续集。

Observable里有很多的operators,如果我们把Observable想象为一个event stream,那么修改这个stream就成为了常见的需求,transformation operators就是为此而生。

在该题目中, 你需要实现实现map(),就如其名,它把value map到另外一个value,从而生成一个新的event stream。

这是一个例子

const source = Observable.from([1,2,3])

map(x => x * x)(source) // this transformer doubles numbers and create a new stream
 .subscribe(console.log)
// 1
// 4
// 9

Observable有一个pipe() 方法可以让其更具可读性。

const source = Observable.from([1,2,3])

source.pipe(map(x => x * x))
 .subscribe(console.log)
// 1
// 4
// 9

注意

Observable 已经提供给你了,可以直接使用。

普通解法但是bugfree胜于奇技淫巧但是各种bug。

(21)