Skip to content

chunkBySize #258

Closed
Closed

Description

@BrianVallelunga

I see chunkBySize is supposed to be added. Is there any guidance or reference for patching this in myself? I've come across a use for it in a pipeline.

Thanks

Activity

  1. BrianVallelunga commented on Jun 19, 2024

    @BrianVallelunga
    Author

    This code seems to work, but I feel like it could be simplified.

    let chunkBySize chunkSize (source: TaskSeq<_>) =
        let enumerator = source.GetAsyncEnumerator()
    
        let checkChunkIndexAndMoveNextAsync chunkIndex =
           if chunkIndex < chunkSize then
               enumerator.MoveNextAsync()
           else
               ValueTask.False
    
        taskSeq {
            let nextChunk () =
                taskSeq {
                    let res = Array.zeroCreate chunkSize
                    res[0] <- enumerator.Current
                    let mutable i = 1
    
                    while! (checkChunkIndexAndMoveNextAsync i) do
                        let current = enumerator.Current
                        res[i] <- current
                        i <- i + 1
    
                    if i = chunkSize then
                        res
                    else
                        Array.sub res 0 i
                }
    
            while! enumerator.MoveNextAsync() do
                yield! nextChunk()
        }
  2. abelbraaksma commented on Jul 10, 2024

    @abelbraaksma
    Member

    Hey @BrianVallelunga, thanks for reporting this! Indeed, chunkBySize was (and is) on the list of functions to be added from the Seq.xxx functions (i.e., where we try to have the same surface area as F#'s Seq as we can), but we didn't yet get to it (no green checkmark in the overview, sorry).

    Your code is not wrong per se. But you'd want to put the let enumerator = source.GetAsyncEnumerator() inside the taskSeq and use use, not let (this will only work in taskSeq or task, outside it, the use is not recognized). Otherwise, the lifecycle of the enumerator is not guarded by the taskSeq CE and this may lead to unwanted side effects.

    You can also add a finalize by hand.

    I'll add this function soon(ish). I may have some time for it this weekend, but it's been a bit busy lately.

  3. added
    good first issue
    topic: surface area
    feature request
    on Jul 10, 2024
  4. added a commit that references this issue on Mar 7, 2026
    9e7e535
  5. added a commit that references this issue on Mar 8, 2026
    7871669
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

    feature requestgood first issuetopic: surface area

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions