diff --git a/src/FSharp.Control.AsyncSeq/AsyncSeq.fs b/src/FSharp.Control.AsyncSeq/AsyncSeq.fs index 688f61c..8cd2bfb 100644 --- a/src/FSharp.Control.AsyncSeq/AsyncSeq.fs +++ b/src/FSharp.Control.AsyncSeq/AsyncSeq.fs @@ -1285,6 +1285,8 @@ module AsyncSeq = let! moven = ie.MoveNext() b := moven else b := None } + + let truncate count source = take count source let skip count (source : AsyncSeq<'T>) : AsyncSeq<_> = asyncSeq { if (count < 0) then invalidArg "count" "must be non-negative" diff --git a/src/FSharp.Control.AsyncSeq/AsyncSeq.fsi b/src/FSharp.Control.AsyncSeq/AsyncSeq.fsi index d177d77..cd2d1be 100644 --- a/src/FSharp.Control.AsyncSeq/AsyncSeq.fsi +++ b/src/FSharp.Control.AsyncSeq/AsyncSeq.fsi @@ -397,8 +397,12 @@ module AsyncSeq = val skipWhile : predicate:('T -> bool) -> source:AsyncSeq<'T> -> AsyncSeq<'T> /// Returns the first N elements of an asynchronous sequence + /// does not cast an exception if count is larger than the sequence length. val take : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T> + /// Alias for take + val truncate : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T> + /// Skips the first N elements of an asynchronous sequence and /// then returns the rest of the sequence unmodified. val skip : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T> diff --git a/tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs b/tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs index aed75d4..fee7b74 100644 --- a/tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs +++ b/tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs @@ -1401,6 +1401,16 @@ let ``AsyncSeq.take should work``() = let ls = ss |> AsyncSeq.toList () +[] +let ``AsyncSeq.truncate should work like take``() = + let s = asyncSeq { + yield ["a",1] |> Map.ofList + } + let expected = s |> AsyncSeq.take 1 + let actual = s |> AsyncSeq.truncate 1 + Assert.AreEqual(expected, actual) + + [] let ``AsyncSeq.mapAsyncParallel should maintain order`` () = for i in 0..100 do