Skip to content

Commit ad56659

Browse files
committed
Add truncate as an alias to mimic Seq.
1 parent 9df32a2 commit ad56659

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/FSharp.Control.AsyncSeq/AsyncSeq.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,8 @@ module AsyncSeq =
12861286
let! moven = ie.MoveNext()
12871287
b := moven
12881288
else b := None }
1289+
1290+
let truncate count source = take count source
12891291

12901292
let skip count (source : AsyncSeq<'T>) : AsyncSeq<_> = asyncSeq {
12911293
if (count < 0) then invalidArg "count" "must be non-negative"

src/FSharp.Control.AsyncSeq/AsyncSeq.fsi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,12 @@ module AsyncSeq =
390390
val skipWhile : predicate:('T -> bool) -> source:AsyncSeq<'T> -> AsyncSeq<'T>
391391

392392
/// Returns the first N elements of an asynchronous sequence
393+
/// does not cast an exception if count is larger than the sequence length.
393394
val take : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T>
394395

396+
/// Alias for take
397+
val truncate : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T>
398+
395399
/// Skips the first N elements of an asynchronous sequence and
396400
/// then returns the rest of the sequence unmodified.
397401
val skip : count:int -> source:AsyncSeq<'T> -> AsyncSeq<'T>

tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,16 @@ let ``AsyncSeq.take should work``() =
13881388
let ls = ss |> AsyncSeq.toList
13891389
()
13901390

1391+
[<Test>]
1392+
let ``AsyncSeq.truncate should work like take``() =
1393+
let s = asyncSeq {
1394+
yield ["a",1] |> Map.ofList
1395+
}
1396+
let expected = s |> AsyncSeq.take 1
1397+
let actual = s |> AsyncSeq.truncate 1
1398+
Assert.AreEqual(expected, actual)
1399+
1400+
13911401
[<Test>]
13921402
let ``AsyncSeq.mapAsyncParallel should maintain order`` () =
13931403
for i in 0..100 do

0 commit comments

Comments
 (0)