Skip to content

Instantly share code, notes, and snippets.

@doomspork
Created April 7, 2018 15:17
Show Gist options
  • Save doomspork/4514f0794f5d7eea7166eb34334e29a2 to your computer and use it in GitHub Desktop.
Save doomspork/4514f0794f5d7eea7166eb34334e29a2 to your computer and use it in GitHub Desktop.
defmodule Example do
def split(list, chunk_size) do
do_split(list, chunk_size, [[]])
end
defp do_split([], _chunk_size, acc) do
acc
end
defp do_split([item|rest], chunk_size, [cur|acc]) do
if length(cur) == chunk_size do
do_split(rest, chunk_size, [[item] | [cur | acc]])
else
do_split(rest, chunk_size, [[item | cur] | acc])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment