Skip to content

Commit

Permalink
Add AsyncSeq.head and AsyncSeq.tail
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb9 committed Nov 30, 2022
1 parent 24c0e83 commit c2ed6d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/FSharp.Control.AsyncSeq/AsyncSeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,13 @@ module AsyncSeq =
let! moven = ie.MoveNext()
b := moven }

let head (source : AsyncSeq<'T>) = async {
match! tryFirst source with
| Some x -> return x
| None -> return invalidArg (nameof source) "The input sequence was empty." }

let tail (source: AsyncSeq<'T>) = skip 1 source

let toArrayAsync (source : AsyncSeq<'T>) : Async<'T[]> = async {
let ra = (new ResizeArray<_>())
use ie = source.GetEnumerator()
Expand Down
7 changes: 7 additions & 0 deletions src/FSharp.Control.AsyncSeq/AsyncSeq.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@ module AsyncSeq =
/// then returns the rest of the sequence unmodified.
val skip : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T>

/// Returns the first element of the asynchronous sequence
val head : source:AsyncSeq<'T> -> Async<'T>

/// Returns an asynchronous sequence that skips 1 element of the underlying
/// sequence and then yields the remaining elements of the sequence.
val tail : source:AsyncSeq<'T> -> AsyncSeq<'T>

/// Creates an async computation which iterates the AsyncSeq and collects the output into an array.
val toArrayAsync : source:AsyncSeq<'T> -> Async<'T []>

Expand Down

0 comments on commit c2ed6d0

Please sign in to comment.