Joseph
Joseph

Reputation: 251

How to break collection up into smaller groups - Elixir

How to breakup this collection [1, 2, 3, 4, 5, 6] into smaller groups? to get this [[1, 2, 3], [4, 5, 6]]

Upvotes: 1

Views: 64

Answers (1)

Joseph
Joseph

Reputation: 251

Use Enum.chunk_every/2 to split it by chunks

Enum.chunk_every([1, 2, 3, 4, 5, 6], 3)
#⇒ [[1, 2, 3], [4, 5, 6]]

or Enum.split/2 to split it into a tuple containing two parts.

Enum.split([1, 2, 3, 4, 5, 6], 3)
#⇒ {[1, 2, 3], [4, 5, 6]}

Upvotes: 7

Related Questions