File tree Expand file tree Collapse file tree 5 files changed +70
-1
lines changed
FSharp.Control.TaskSeq.Test Expand file tree Collapse file tree 5 files changed +70
-1
lines changed Original file line number Diff line number Diff line change @@ -83,7 +83,7 @@ The following is the progress report:
83
83
| | ` compareWith ` | ` compareWith ` | ` compareWithAsync ` | |
84
84
| ✅ ; [ #69 ] [ ] | ` concat ` | ` concat ` | | |
85
85
| ✅ ; [ #70 ] [ ] | ` contains ` | ` contains ` | | |
86
- | | ` delay ` | ` delay ` | | |
86
+ | &#x 2705 ; [ # 82 ] [ ] | ` delay ` | ` delay ` | | |
87
87
| | ` distinct ` | ` distinct ` | | |
88
88
| | ` distinctBy ` | ` dictinctBy ` | ` distinctByAsync ` | |
89
89
| ✅ ; [ #2 ] [ ] | ` empty ` | ` empty ` | | |
@@ -610,4 +610,5 @@ module TaskSeq =
610
610
[ #70 ] : https://github.com/fsprojects/FSharp.Control.TaskSeq/pull/70
611
611
[ #76 ] : https://github.com/fsprojects/FSharp.Control.TaskSeq/pull/76
612
612
[ #81 ] : https://github.com/fsprojects/FSharp.Control.TaskSeq/pull/81
613
+ [ #82 ] : https://github.com/fsprojects/FSharp.Control.TaskSeq/pull/82
613
614
Original file line number Diff line number Diff line change 18
18
<Compile Include =" TaskSeq.Collect.Tests.fs" />
19
19
<Compile Include =" TaskSeq.Concat.Tests.fs" />
20
20
<Compile Include =" TaskSeq.Contains.Tests.fs" />
21
+ <Compile Include =" TaskSeq.Delay.Tests.fs" />
21
22
<Compile Include =" TaskSeq.Empty.Tests.fs" />
22
23
<Compile Include =" TaskSeq.ExactlyOne.Tests.fs" />
23
24
<Compile Include =" TaskSeq.Exists.Tests.fs" />
Original file line number Diff line number Diff line change
1
+ module TaskSeq.Tests.Delay
2
+
3
+ open System
4
+
5
+ open Xunit
6
+ open FsUnit.Xunit
7
+ open FsToolkit.ErrorHandling
8
+
9
+ open FSharp.Control
10
+ open System.Collections .Generic
11
+
12
+ //
13
+ // TaskSeq.delay
14
+ //
15
+
16
+ let validateSequence ts =
17
+ ts
18
+ |> TaskSeq.toSeqCachedAsync
19
+ |> Task.map ( Seq.map string)
20
+ |> Task.map ( String.concat " " )
21
+ |> Task.map ( should equal " 12345678910" )
22
+
23
+ module EmptySeq =
24
+ [<Theory; ClassData( typeof< TestEmptyVariants>) >]
25
+ let ``TaskSeq - delay with empty sequences`` variant =
26
+ fun () -> Gen.getEmptyVariant variant
27
+ |> TaskSeq.delay
28
+ |> verifyEmpty
29
+
30
+ module Immutable =
31
+ [<Theory; ClassData( typeof< TestImmTaskSeq>) >]
32
+ let ``TaskSeq - delay`` variant =
33
+ fun () -> Gen.getSeqImmutable variant
34
+ |> TaskSeq.delay
35
+ |> validateSequence
36
+
37
+ module SideEffect =
38
+ [<Fact>]
39
+ let ``TaskSeq - delay executes side effects`` () = task {
40
+ let mutable i = 0
41
+
42
+ let ts =
43
+ fun () -> taskSeq {
44
+ yield ! [ 1 .. 10 ]
45
+ i <- i + 1
46
+ }
47
+ |> TaskSeq.delay
48
+
49
+ do ! ts |> validateSequence
50
+ i |> should equal 1
51
+ let! len = TaskSeq.length ts
52
+ i |> should equal 2 // re-eval of the sequence executes side effect again
53
+ len |> should equal 10
54
+ }
Original file line number Diff line number Diff line change @@ -164,6 +164,11 @@ module TaskSeq =
164
164
let initAsync count initializer = Internal.init ( Some count) ( InitActionAsync initializer)
165
165
let initInfiniteAsync initializer = Internal.init None ( InitActionAsync initializer)
166
166
167
+ let delay ( generator : unit -> taskSeq < 'T >) =
168
+ { new IAsyncEnumerable< 'T> with
169
+ member _.GetAsyncEnumerator ( ct ) = generator() .GetAsyncEnumerator( ct)
170
+ }
171
+
167
172
let concat ( sources : taskSeq < #taskSeq < 'T >>) = taskSeq {
168
173
for ts in sources do
169
174
yield ! ( ts :> taskSeq < 'T >)
Original file line number Diff line number Diff line change @@ -39,6 +39,14 @@ module TaskSeq =
39
39
/// </summary>
40
40
val lengthByAsync : predicate : ( 'T -> #Task < bool >) -> source : taskSeq < 'T > -> Task < int >
41
41
42
+ /// <summary>
43
+ /// Returns a task sequence that is given by the delayed specification of a task sequence.
44
+ /// </summary>
45
+ ///
46
+ /// <param name="generator">The generating function for the task sequence.</param>
47
+ /// <returns>The generated task sequence.</returns>
48
+ val delay : generator : ( unit -> taskSeq < 'T >) -> taskSeq < 'T >
49
+
42
50
/// <summary>
43
51
/// Generates a new task sequence which, when iterated, will return successive elements by calling the given function
44
52
/// with the current index, up to the given count. Each element is saved after its initialization for successive access to
You can’t perform that action at this time.
0 commit comments