Skip to content

Commit 12de7f7

Browse files
committed
Move implementation of singleton to 'internals' file, update doc comment and remove exception
1 parent 51cdad9 commit 12de7f7

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/FSharp.Control.TaskSeq/TaskSeq.fs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff 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

src/FSharp.Control.TaskSeq/TaskSeq.fsi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff 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.

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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())

0 commit comments

Comments
 (0)