Skip to content

Add new function: chunks #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Daniel-Diaz
Copy link

This function splits a vector into subvectors of equal length.

Example interaction

We bring a vector into scope for our testing.

ghci> let v = Data.Vector.Sized.iterateN (+1) 1 :: Data.Vector.Sized.Vector 6 Int
ghci> v
Vector [1,2,3,4,5,6]

Now we split the vector. How it's split is determined by the output type.

ghci> Data.Vector.Sized.chunks v :: Data.Vector.Sized.Vector 3 (Data.Vector.Sized.Vector 2 Int)
Vector [Vector [1,2],Vector [3,4],Vector [5,6]]
ghci> Data.Vector.Sized.chunks v :: Data.Vector.Sized.Vector 2 (Data.Vector.Sized.Vector 3 Int)
Vector [Vector [1,2,3],Vector [4,5,6]]
ghci> Data.Vector.Sized.chunks v :: Data.Vector.Sized.Vector 1 (Data.Vector.Sized.Vector 6 Int)
Vector [Vector [1,2,3,4,5,6]]

If the output size is incorrect, a type error is thrown:

ghci> Data.Vector.Sized.chunks v :: Data.Vector.Sized.Vector 3 (Data.Vector.Sized.Vector 4 Int)

<interactive>:9:26: error:
     Couldn't match type 6 with 12
      Expected: Data.Vector.Sized.Vector (3 * 4) Int
        Actual: Data.Vector.Sized.Vector 6 Int
     In the first argument of Data.Vector.Sized.chunks, namely v
      In the expression:
          Data.Vector.Sized.chunks v ::
            Data.Vector.Sized.Vector 3 (Data.Vector.Sized.Vector 4 Int)
      In an equation for it’:
          it
            = Data.Vector.Sized.chunks v ::
                Data.Vector.Sized.Vector 3 (Data.Vector.Sized.Vector 4 Int)

Related functionality

The function chunks is the inverse of concatMap id.

Naming

I chose chunks instead of the more common chunksOf because the usual length argument is missing. I can change it on request.

@kozross
Copy link
Collaborator

kozross commented May 29, 2025

@Daniel-Diaz - thanks for the PR! It looks like our CI is in dire need of an upgrade, but fundamentally I think your idea is great. I'll fix the CI at some point in a separate PR, and then we can merge your work.

@expipiplus1 - does this seem reasonable to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants