You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _chapters/functionalreactiveprogramming.md
+39-35Lines changed: 39 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,12 +53,13 @@ You could think of our lazy sequences as being “pull-based” data structures,
53
53
Just as we have done for various data structures (arrays and so on) in previous chapters, we can define a transform over an Observable to create a new Observable. This transformation may have multiple steps the same way that we chained `filter` and `map` operations over arrays previously. In RxJS’s Observable implementation, however, they’ve gone a little bit more functional, by insisting that such operations are composed (rather than chained) inside a `pipe`. For example, here’s the squares of even numbers in the range [0,10):
54
54
55
55
```javascript
56
-
constisEven=x=>x%2===0,
57
-
square=x=>x*x
56
+
constisEven=x=>x%2===0,
57
+
square=x=>x*x
58
58
range(10)
59
59
.pipe(
60
60
filter(isEven),
61
-
map(square))
61
+
map(square)
62
+
)
62
63
.subscribe(console.log)
63
64
```
64
65
@@ -87,9 +88,10 @@ To solve the first Project Euler problem using RxJS, we generate a sequence of n
87
88
```javascript
88
89
range(1000)
89
90
.pipe(
90
-
filter(x=> x%3===0|| x%5===0),
91
-
scan((a,v)=>a+v),
92
-
last())
91
+
filter(x=> x%3===0|| x%5===0),
92
+
scan((a,v) => a+v),
93
+
last()
94
+
)
93
95
.subscribe(console.log);
94
96
```
95
97
@@ -125,8 +127,8 @@ By contrast, the `mergeMap` operator gives the *Cartesian product* of two stream
125
127
126
128
```javascript
127
129
columns.pipe(
128
-
mergeMap(column=>rows.pipe(
129
-
map(row=>[column, row])
130
+
mergeMap(column=>rows.pipe(
131
+
map(row=>[column, row])
130
132
))
131
133
).subscribe(console.log)
132
134
```
@@ -147,8 +149,8 @@ If we contrast `mergeMap` and `map`, `map` will produce an Observable of Observa
147
149
148
150
```javascript
149
151
columns.pipe(
150
-
map(column=>rows.pipe(
151
-
map(row=>[column, row])
152
+
map(column=>rows.pipe(
153
+
map(row=>[column, row])
152
154
))
153
155
).subscribe(console.log)
154
156
```
@@ -191,7 +193,7 @@ The following lets us see in the console the keys pressed as they come in, it wi
191
193
192
194
```javascript
193
195
key$.pipe(
194
-
map(e=>e.key)
196
+
map(e=>e.key)
195
197
).subscribe(console.log)
196
198
```
197
199
@@ -203,7 +205,7 @@ The following prints “!!” on every mousedown:
203
205
204
206
```javascript
205
207
mouse$.pipe(
206
-
map(_=>"!!")
208
+
map(_=>"!!")
207
209
).subscribe(console.log)
208
210
```
209
211
@@ -216,8 +218,8 @@ Once again this will keep producing the message for every mouse click for as lon
216
218
The following achieves the same thing with a single subscription using `merge`:
217
219
218
220
```javascript
219
-
merge(key$.pipe(map(e=>e.key)),
220
-
mouse$.pipe(map(_=>"!!"))
221
+
merge(key$.pipe(map(e=>e.key)),
222
+
mouse$.pipe(map(_=>"!!"))
221
223
).subscribe(console.log)
222
224
```
223
225
@@ -266,7 +268,7 @@ Creating new Observable streams from existing streams
@@ -427,38 +429,38 @@ the position of the top-left corner of the rectangle, and (optionally, since it
427
429
428
430
```typescript
429
431
typeState=Readonly<{
430
-
pos:Point,
431
-
offset?:Point
432
+
pos:Point,
433
+
offset?:Point
432
434
}>
433
435
```
434
436
435
437
We’ll introduce some types to model the objects coming through the stream and the effects they have when applied to a `State` object in the `scan`. First, all the events we care about have a position on the SVG canvas associated with them, so we’ll have a simple immutable `Point` interface with `x` and `y` positions and a couple of handy vector math methods (note that these create a new `Point` rather than mutating any existing state within the `Point`):
Now we create a subclass of `Point` with a constructor letting us instantiate it for a given (DOM) `MouseEvent` and an `abstract` (placeholder) definition for a function to apply the correct update action to the `State`:
0 commit comments