Elm List like Elixir Enum.chunk

Hello all!

What is the best way to something similar to what Enum.chunk does in elixir?
Let’s say that I have a list and I want to take every 7 items and paint them.

Thank you!

I’m happy to answer your question, but I think we may be facing an X/Y problem. What are you trying to accomplish? There may be a specific piece of knowledge someone has that may solve the problem you’re actually having. :slight_smile:

That said, here’s how you’d chunk a list into groups of seven, with a list of whatever remains at the end.

sevens : List a -> List (List a)
sevens mapper items =
    case items of
        a :: b :: c :: d :: e :: f :: g :: rest ->
            [ a, b, c, d, e, f, g ] : sevens rest

        _ ->
1 Like

I might try it another way. Here’s an Ellie: https://ellie-app.com/nWknYsnhNa1/1 :slight_smile:

I believe List.Extra.greedyGroupsOf also does what you’re asking.