Skip to content

Commit c2ed6d0

Browse files
committed
Add AsyncSeq.head and AsyncSeq.tail
1 parent 24c0e83 commit c2ed6d0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/FSharp.Control.AsyncSeq/AsyncSeq.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,13 @@ module AsyncSeq =
13441344
let! moven = ie.MoveNext()
13451345
b := moven }
13461346

1347+
let head (source : AsyncSeq<'T>) = async {
1348+
match! tryFirst source with
1349+
| Some x -> return x
1350+
| None -> return invalidArg (nameof source) "The input sequence was empty." }
1351+
1352+
let tail (source: AsyncSeq<'T>) = skip 1 source
1353+
13471354
let toArrayAsync (source : AsyncSeq<'T>) : Async<'T[]> = async {
13481355
let ra = (new ResizeArray<_>())
13491356
use ie = source.GetEnumerator()

src/FSharp.Control.AsyncSeq/AsyncSeq.fsi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ module AsyncSeq =
417417
/// then returns the rest of the sequence unmodified.
418418
val skip : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T>
419419

420+
/// Returns the first element of the asynchronous sequence
421+
val head : source:AsyncSeq<'T> -> Async<'T>
422+
423+
/// Returns an asynchronous sequence that skips 1 element of the underlying
424+
/// sequence and then yields the remaining elements of the sequence.
425+
val tail : source:AsyncSeq<'T> -> AsyncSeq<'T>
426+
420427
/// Creates an async computation which iterates the AsyncSeq and collects the output into an array.
421428
val toArrayAsync : source:AsyncSeq<'T> -> Async<'T []>
422429

0 commit comments

Comments
 (0)