Skip to content

Add AsyncSeq.chunkBySize #175

Closed
Closed

Description

@brianberns

Just like Seq.chunkBySize. Possible implementation:

    let chunkBySize (chunkSize : int) (source: AsyncSeq<'a>) =
        asyncSeq {
            use enumerator = source.GetEnumerator()
            let mutable isFinished = false
            while not isFinished do
                let chunk = ResizeArray<'a>(chunkSize)
                while chunk.Count < chunkSize && not isFinished do
                    match! enumerator.MoveNext() with
                        | Some item -> chunk.Add(item)
                        | None -> isFinished <- true
                if chunk.Count > 0 then
                    yield chunk.ToArray()
        }

Activity

  1. njlr commented on Sep 9, 2025

    @njlr
    Contributor

    Provided by bufferByCount

    let bufferByCount (bufferSize:int) (source:AsyncSeq<'T>) : AsyncSeq<'T[]> =
    if (bufferSize < 1) then invalidArg "bufferSize" "must be positive"
    asyncSeq {
    let buffer = new ResizeArray<_>()
    use ie = source.GetEnumerator()
    let! move = ie.MoveNext()
    let b = ref move
    while b.Value.IsSome do
    buffer.Add b.Value.Value
    if buffer.Count = bufferSize then
    yield buffer.ToArray()
    buffer.Clear()
    let! moven = ie.MoveNext()
    b := moven
    if (buffer.Count > 0) then
    yield buffer.ToArray() }

    Suggest closing this.

  2. brianberns commented on Sep 9, 2025

    @brianberns
    Author

    Ah, that's good to know. Thank you. I would still suggest adding chunkBySize as a synonym, however, since that is now the standard F# name for a function with this behavior.

  3. njlr commented on Sep 9, 2025

    @njlr
    Contributor

    Related fsprojects/FSharp.Control.TaskSeq#265

    @dsyme @abelbraaksma I think these should be aligned. Any preference?

  4. dsyme commented on Oct 24, 2025

    @dsyme
    Contributor

    @njlr I agree this would be good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions