Last active
September 14, 2015 22:29
-
-
Save dramsay/f1f1abe989b6616d7668 to your computer and use it in GitHub Desktop.
Implementation of each_cons in Elixir by Dave Thomas (from 2013)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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