-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTaskSeqCombining.fsx
More file actions
358 lines (215 loc) · 8.26 KB
/
TaskSeqCombining.fsx
File metadata and controls
358 lines (215 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
(**
---
title: Combining Task Sequences
category: Documentation
categoryindex: 2
index: 5
description: How to combine, slice and reshape F# task sequences using append, zip, zipWith, splitAt, take, skip, chunkBySize, chunkBy, windowed and related combinators.
keywords: F#, task sequences, TaskSeq, IAsyncEnumerable, append, zip, zip3, zipWith, zipWith3, splitAt, take, skip, drop, truncate, takeWhile, skipWhile, chunkBySize, chunkBy, windowed, pairwise, concat
---
*)
(*** condition: prepare ***)
#nowarn "211"
#I "../src/FSharp.Control.TaskSeq/bin/Release/netstandard2.1"
#r "FSharp.Control.TaskSeq.dll"
(*** condition: fsx ***)
#if FSX
#r "nuget: FSharp.Control.TaskSeq,{{fsdocs-package-version}}"
#endif // FSX
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: FSharp.Control.TaskSeq,{{fsdocs-package-version}}"
#endif // IPYNB
(**
# Combining Task Sequences
This page covers operations that combine multiple sequences or reshape a single sequence: append,
zip, zipWith, concat, slicing with take/skip/splitAt, chunking and windowing.
*)
open FSharp.Control
(**
---
## Append
`TaskSeq.append` produces all elements of the first sequence followed by all elements of the
second. The second sequence does not start until the first is exhausted:
*)
let first = TaskSeq.ofList [ 1; 2; 3 ]
let second = TaskSeq.ofList [ 4; 5; 6 ]
let appended : TaskSeq<int> = TaskSeq.append first second // 1, 2, 3, 4, 5, 6
(**
Inside `taskSeq { ... }`, `yield!` is the natural way to concatenate:
*)
let combined = taskSeq {
yield! first
yield! second
}
(**
`TaskSeq.appendSeq` appends a plain `seq<'T>` after a task sequence.
`TaskSeq.prependSeq` prepends a plain `seq<'T>` before a task sequence:
*)
let withPrefix : TaskSeq<int> = TaskSeq.prependSeq [ 0 ] first // 0, 1, 2, 3
let withSuffix : TaskSeq<int> = TaskSeq.appendSeq first [ 4; 5 ] // 1, 2, 3, 4, 5
(**
---
## concat
`TaskSeq.concat` flattens a task sequence of task sequences into a single flat sequence.
Each inner sequence is consumed fully before the next one begins:
*)
let nested : TaskSeq<TaskSeq<int>> =
TaskSeq.ofList
[ TaskSeq.ofList [ 1; 2 ]
TaskSeq.ofList [ 3; 4 ]
TaskSeq.ofList [ 5; 6 ] ]
let flat : TaskSeq<int> = TaskSeq.concat nested // 1, 2, 3, 4, 5, 6
(**
Overloads also exist for `TaskSeq<seq<'T>>`, `TaskSeq<'T list>`, `TaskSeq<'T[]>`, and
`TaskSeq<ResizeArray<'T>>`.
---
## zip and zip3
`TaskSeq.zip` pairs up elements from two sequences, stopping when the shorter sequence ends:
*)
let letters : TaskSeq<char> = TaskSeq.ofList [ 'a'; 'b'; 'c' ]
let nums : TaskSeq<int> = TaskSeq.ofList [ 1; 2; 3; 4 ]
let pairs : TaskSeq<char * int> = TaskSeq.zip letters nums
// ('a',1), ('b',2), ('c',3) — stops when letters runs out
(**
`TaskSeq.zip3` does the same for three sequences:
*)
let booleans : TaskSeq<bool> = TaskSeq.ofList [ true; false; true ]
let triples : TaskSeq<char * int * bool> = TaskSeq.zip3 letters nums booleans
(**
---
## zipWith and zipWithAsync
`TaskSeq.zipWith` is like `zip` but applies a mapping function to produce a result instead of
yielding a tuple. The result sequence stops when the shorter source ends:
*)
let addPairs : TaskSeq<int> = TaskSeq.zipWith (+) nums nums
// 2, 4, 6, 8
(**
`TaskSeq.zipWithAsync` accepts an asynchronous mapping function:
*)
let asyncProduct : TaskSeq<int> =
TaskSeq.zipWithAsync (fun a b -> Task.fromResult (a * b)) nums nums
// 1, 4, 9, 16, ...
(**
---
## zipWith3 and zipWithAsync3
`TaskSeq.zipWith3` combines three sequences with a three-argument mapping function, stopping at
the shortest:
*)
let sumThree : TaskSeq<int> =
TaskSeq.zipWith3 (fun a b c -> a + b + c) nums nums nums
// 3, 6, 9, 12, ...
(**
`TaskSeq.zipWithAsync3` takes an asynchronous three-argument mapper:
*)
let asyncSumThree : TaskSeq<int> =
TaskSeq.zipWithAsync3 (fun a b c -> Task.fromResult (a + b + c)) nums nums nums
(**
---
## pairwise
`TaskSeq.pairwise` produces a sequence of consecutive pairs. An input with fewer than two elements
produces an empty result:
*)
let consecutive : TaskSeq<int> = TaskSeq.ofList [ 1; 2; 3; 4; 5 ]
let pairs2 : TaskSeq<int * int> = consecutive |> TaskSeq.pairwise
// (1,2), (2,3), (3,4), (4,5)
(**
---
## take and truncate
`TaskSeq.take count` yields exactly `count` elements and throws if the source is shorter:
*)
let first3 : TaskSeq<int> = consecutive |> TaskSeq.take 3 // 1, 2, 3
(**
`TaskSeq.truncate count` yields _at most_ `count` elements without throwing when the source is
shorter:
*)
let atMost10 : TaskSeq<int> = consecutive |> TaskSeq.truncate 10 // 1, 2, 3, 4, 5
(**
---
## splitAt
`TaskSeq.splitAt count` splits a sequence into a prefix array and a lazy remainder sequence. The
prefix always contains _at most_ `count` elements — it never throws when the sequence is shorter.
The remainder sequence is a lazy view over the unconsumed tail and can be iterated once:
*)
let splitData : TaskSeq<int> = TaskSeq.ofList [ 1..10 ]
let splitExample : Task<int[] * TaskSeq<int>> = TaskSeq.splitAt 4 splitData
// prefix = [|1;2;3;4|], rest = lazy 5,6,7,8,9,10
(**
Unlike `take`/`skip`, a single `splitAt` call evaluates elements only once — the prefix is
materialised eagerly and the rest is yielded lazily without re-reading the source.
---
## skip and drop
`TaskSeq.skip count` skips exactly `count` elements and throws if the source is shorter:
*)
let afterFirst2 : TaskSeq<int> = consecutive |> TaskSeq.skip 2 // 3, 4, 5
(**
`TaskSeq.drop count` drops _at most_ `count` elements without throwing:
*)
let safeAfter10 : TaskSeq<int> = consecutive |> TaskSeq.drop 10 // empty
(**
---
## takeWhile and takeWhileInclusive
`TaskSeq.takeWhile predicate` yields elements while the predicate is `true`, then stops (the
element that caused the stop is **not** yielded):
*)
let lessThan4 : TaskSeq<int> = consecutive |> TaskSeq.takeWhile (fun n -> n < 4)
// 1, 2, 3
(**
`TaskSeq.takeWhileInclusive` yields the first element for which the predicate is `false` and
then stops — so at least one element is always yielded from a non-empty source:
*)
let upToFirstGe4 : TaskSeq<int> =
consecutive |> TaskSeq.takeWhileInclusive (fun n -> n < 4)
// 1, 2, 3, 4
(**
Async variants: `TaskSeq.takeWhileAsync`, `TaskSeq.takeWhileInclusiveAsync`.
---
## skipWhile and skipWhileInclusive
`TaskSeq.skipWhile predicate` skips elements while the predicate is `true`, then yields the
rest (the first failing element **is** yielded):
*)
let from3 : TaskSeq<int> = consecutive |> TaskSeq.skipWhile (fun n -> n < 3)
// 3, 4, 5
(**
`TaskSeq.skipWhileInclusive` also skips the first element for which the predicate is `false`:
*)
let afterFirst3 : TaskSeq<int> =
consecutive |> TaskSeq.skipWhileInclusive (fun n -> n < 3)
// 4, 5
(**
Async variants: `TaskSeq.skipWhileAsync`, `TaskSeq.skipWhileInclusiveAsync`.
---
## chunkBySize
`TaskSeq.chunkBySize chunkSize` divides the sequence into non-overlapping arrays of at most
`chunkSize` elements. The last chunk may be smaller if the sequence does not divide evenly:
*)
let chunks : TaskSeq<int[]> = consecutive |> TaskSeq.chunkBySize 2
// [|1;2|], [|3;4|], [|5|]
(**
---
## chunkBy and chunkByAsync
`TaskSeq.chunkBy projection` groups _consecutive_ elements with the same key into `(key, elements[])` pairs.
A new group starts each time the key changes. Unlike `groupBy`, elements that are not adjacent are
**not** merged, so the source order is preserved and the sequence can be infinite:
*)
let words : TaskSeq<string> = TaskSeq.ofList [ "apple"; "apricot"; "banana"; "blueberry"; "cherry" ]
let byFirstLetter : TaskSeq<char * string[]> =
words |> TaskSeq.chunkBy (fun w -> w[0])
// ('a', [|"apple";"apricot"|]), ('b', [|"banana";"blueberry"|]), ('c', [|"cherry"|])
(**
`TaskSeq.chunkByAsync` accepts an asynchronous projection:
*)
let byFirstLetterAsync : TaskSeq<char * string[]> =
words |> TaskSeq.chunkByAsync (fun w -> Task.fromResult w[0])
(**
---
## windowed
`TaskSeq.windowed windowSize` produces a sliding window of exactly `windowSize` consecutive
elements. The result is empty if the source has fewer elements than the window size:
*)
let windows : TaskSeq<int[]> = consecutive |> TaskSeq.windowed 3
// [|1;2;3|], [|2;3;4|], [|3;4;5|]
(**
`windowed` uses a ring buffer internally, so each window allocation is separate — safe to
store the windows independently.
*)