Skip to content

Instantly share code, notes, and snippets.

@dramsay
Last active September 14, 2015 22:29
Show Gist options
  • Save dramsay/f1f1abe989b6616d7668 to your computer and use it in GitHub Desktop.
Save dramsay/f1f1abe989b6616d7668 to your computer and use it in GitHub Desktop.
Implementation of each_cons in Elixir by Dave Thomas (from 2013)
# updated to reflect current syntax for default params
defmodule DaveCons do
def each_cons(list, n \\ 2), do: _each_cons(list, n, [])
defp _each_cons(list, n, result) when length(list) < n do
Enum.reverse result
end
defp _each_cons(list = [_ | tail], n, result) do
_each_cons(tail, n, [Enum.take(list, n)|result])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment