File tree Expand file tree Collapse file tree 3 files changed +22
-16
lines changed
src/FSharp.Control.TaskSeq Expand file tree Collapse file tree 3 files changed +22
-16
lines changed Original file line number Diff line number Diff line change @@ -21,19 +21,7 @@ module TaskSeq =
2121 }
2222 }
2323
24- let singleton ( source : 'T ) =
25- { new IAsyncEnumerable< 'T> with
26- member _.GetAsyncEnumerator ( _ ) =
27- let mutable started = false
28- { new IAsyncEnumerator< 'T> with
29- member _.MoveNextAsync () =
30- let r = ValueTask.FromResult ( not started)
31- started <- true
32- r
33- member _.get_Current () : 'T = if started then source else invalidOp " Enumeration has not started. Call MoveNextAsync."
34- member _.DisposeAsync () = ValueTask.CompletedTask
35- }
36- }
24+ let singleton ( source : 'T ) = Internal.singleton source
3725
3826 let isEmpty source = Internal.isEmpty source
3927
Original file line number Diff line number Diff line change @@ -7,9 +7,11 @@ module TaskSeq =
77
88 /// Initialize an empty taskSeq.
99 val empty < 'T > : taskSeq < 'T >
10-
11- /// Creates a taskSeq sequence that generates a single element and then ends.
12- val singleton : source : 'T -> taskSeq < 'T >
10+
11+ /// <summary>
12+ /// Creates a <see cref="taskSeq" /> sequence from <paramref name="source" /> that generates a single element and then ends.
13+ /// </summary>
14+ val singleton : source : 'T -> taskSeq < 'T >
1315
1416 /// <summary>
1517 /// Returns <see cref="true" /> if the task sequence contains no elements, <see cref="false" /> otherwise.
Original file line number Diff line number Diff line change @@ -61,6 +61,22 @@ module internal TaskSeqInternal =
6161 return not step
6262 }
6363
64+ let singleton ( source : 'T ) =
65+ { new IAsyncEnumerable< 'T> with
66+ member _.GetAsyncEnumerator ( _ ) =
67+ let mutable ended = false
68+
69+ { new IAsyncEnumerator< 'T> with
70+ member _.MoveNextAsync () =
71+ let vt = ValueTask.FromResult( not ended)
72+ ended <- true
73+ vt
74+
75+ member _.Current : 'T = if ended then Unchecked.defaultof< 'T> else source
76+ member _.DisposeAsync () = ValueTask.CompletedTask
77+ }
78+ }
79+
6480 /// Returns length unconditionally, or based on a predicate
6581 let lengthBy predicate ( source : taskSeq < _ >) = task {
6682 use e = source.GetAsyncEnumerator( CancellationToken())
You can’t perform that action at this time.
0 commit comments