Skip to content

Commit 2a5a320

Browse files
authored
Merge pull request #204 from njlr/feature/issue-175-chunk-by-size-rename
feature/issue-175-chunk-by-size-rename
2 parents 9d29d7e + 2579d3e commit 2a5a320

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/FSharp.Control.AsyncSeq/AsyncSeq.fs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,23 +1537,27 @@ module AsyncSeq =
15371537
result <- interleave result x
15381538
result
15391539

1540-
let bufferByCount (bufferSize:int) (source:AsyncSeq<'T>) : AsyncSeq<'T[]> =
1541-
if (bufferSize < 1) then invalidArg "bufferSize" "must be positive"
1540+
let chunkBySize (chunkSize:int) (source:AsyncSeq<'T>) : AsyncSeq<'T[]> =
1541+
if chunkSize < 1 then invalidArg (nameof chunkSize) "must be positive"
15421542
asyncSeq {
15431543
let buffer = new ResizeArray<_>()
15441544
use ie = source.GetEnumerator()
15451545
let! move = ie.MoveNext()
15461546
let b = ref move
15471547
while b.Value.IsSome do
15481548
buffer.Add b.Value.Value
1549-
if buffer.Count = bufferSize then
1549+
if buffer.Count = chunkSize then
15501550
yield buffer.ToArray()
15511551
buffer.Clear()
15521552
let! moven = ie.MoveNext()
15531553
b := moven
15541554
if (buffer.Count > 0) then
15551555
yield buffer.ToArray() }
15561556

1557+
[<Obsolete("Use AsyncSeq.chunkBySize instead")>]
1558+
let bufferByCount (bufferSize:int) (source:AsyncSeq<'T>) : AsyncSeq<'T[]> =
1559+
chunkBySize bufferSize source
1560+
15571561
#if !FABLE_COMPILER
15581562
let toSortedSeq fn source =
15591563
toArrayAsync source |> Async.map fn |> Async.RunSynchronously

src/FSharp.Control.AsyncSeq/AsyncSeq.fsi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ module AsyncSeq =
477477

478478
/// Buffer items from the async sequence into buffers of a specified size.
479479
/// The last buffer returned may be less than the specified buffer size.
480+
val chunkBySize : chunkSize:int -> source:AsyncSeq<'T> -> AsyncSeq<'T []>
481+
482+
/// Buffer items from the async sequence into buffers of a specified size.
483+
/// The last buffer returned may be less than the specified buffer size.
484+
[<Obsolete("Use AsyncSeq.chunkBySize instead")>]
480485
val bufferByCount : bufferSize:int -> source:AsyncSeq<'T> -> AsyncSeq<'T []>
481486

482487
#if !FABLE_COMPILER
@@ -524,7 +529,7 @@ module AsyncSeq =
524529
/// Builds a new asynchronous sequence whose elements are generated by
525530
/// applying the specified function to all elements of the input sequence.
526531
///
527-
/// The function is applied to elements in parallel, and results are emitted
532+
/// The function is applied to elements in parallel, and results are emitted
528533
/// in the order they complete (unordered), without preserving the original order.
529534
/// This can provide better performance than mapAsyncParallel when order doesn't matter.
530535
/// Parallelism is bound by the ThreadPool.

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,31 +436,31 @@ let ``AsyncSeq.interleaveMany 3``() =
436436

437437

438438
[<Test>]
439-
let ``AsyncSeq.bufferByCount``() =
439+
let ``AsyncSeq.chunkBySize``() =
440440
let s = asyncSeq {
441441
yield 1
442442
yield 2
443443
yield 3
444444
yield 4
445445
yield 5
446446
}
447-
let s' = s |> AsyncSeq.bufferByCount 2 |> AsyncSeq.toListSynchronously
447+
let s' = s |> AsyncSeq.chunkBySize 2 |> AsyncSeq.toListSynchronously
448448
Assert.True(([[|1;2|];[|3;4|];[|5|]] = s'))
449449

450450
[<Test>]
451-
let ``AsyncSeq.bufferByCount various sizes``() =
451+
let ``AsyncSeq.chunkBySize various sizes``() =
452452
for sz in 0 .. 10 do
453453
let s = asyncSeq {
454454
for i in 1 .. sz do
455455
yield i
456456
}
457-
let s' = s |> AsyncSeq.bufferByCount 1 |> AsyncSeq.toListSynchronously
457+
let s' = s |> AsyncSeq.chunkBySize 1 |> AsyncSeq.toListSynchronously
458458
Assert.True(([for i in 1 .. sz -> [|i|]] = s'))
459459

460460
[<Test>]
461-
let ``AsyncSeq.bufferByCount empty``() =
461+
let ``AsyncSeq.chunkBySize empty``() =
462462
let s = AsyncSeq.empty<int>
463-
let s' = s |> AsyncSeq.bufferByCount 2 |> AsyncSeq.toListSynchronously
463+
let s' = s |> AsyncSeq.chunkBySize 2 |> AsyncSeq.toListSynchronously
464464
Assert.True(([] = s'))
465465

466466

@@ -2429,20 +2429,20 @@ let ``AsyncSeq.toChannel and AsyncSeq.fromChannel capture exns``() =
24292429
// Additional Coverage Tests targeting uncovered edge cases and branches
24302430

24312431
[<Test>]
2432-
let ``AsyncSeq.bufferByCount with size 1 should work`` () =
2432+
let ``AsyncSeq.chunkBySize with size 1 should work`` () =
24332433
let source = asyncSeq { yield 1; yield 2; yield 3 }
2434-
let result = AsyncSeq.bufferByCount 1 source |> AsyncSeq.toListSynchronously
2434+
let result = AsyncSeq.chunkBySize 1 source |> AsyncSeq.toListSynchronously
24352435
Assert.AreEqual([[|1|]; [|2|]; [|3|]], result)
24362436

24372437
[<Test>]
2438-
let ``AsyncSeq.bufferByCount with empty sequence should return empty`` () =
2439-
let result = AsyncSeq.bufferByCount 2 AsyncSeq.empty |> AsyncSeq.toListSynchronously
2438+
let ``AsyncSeq.chunkBySize with empty sequence should return empty`` () =
2439+
let result = AsyncSeq.chunkBySize 2 AsyncSeq.empty |> AsyncSeq.toListSynchronously
24402440
Assert.AreEqual([], result)
24412441

24422442
[<Test>]
2443-
let ``AsyncSeq.bufferByCount with size larger than sequence should return partial`` () =
2443+
let ``AsyncSeq.chunkBySize with size larger than sequence should return partial`` () =
24442444
let source = asyncSeq { yield 1; yield 2 }
2445-
let result = AsyncSeq.bufferByCount 5 source |> AsyncSeq.toListSynchronously
2445+
let result = AsyncSeq.chunkBySize 5 source |> AsyncSeq.toListSynchronously
24462446
Assert.AreEqual([[|1; 2|]], result)
24472447

24482448
[<Test>]

0 commit comments

Comments
 (0)