Skip to content

Instantly share code, notes, and snippets.

@dramsay
Last active September 14, 2015 12:36
Show Gist options
  • Save dramsay/cf1f0fd425b1d9b376ae to your computer and use it in GitHub Desktop.
Save dramsay/cf1f0fd425b1d9b376ae to your computer and use it in GitHub Desktop.
defmodule Cons do
def each_cons(list, num) when length(list) < num, do: []
def each_cons(list, num) do
[ _ | tail ] = list
cons = Enum.take(list, num)
[ cons | each_cons(tail, num) ]
end
end
iex(1)> Cons.each_cons(String.to_char_list("abacabaca"), 4)
['abac', 'baca', 'acab', 'caba', 'abac', 'baca']
iex(2)> Cons.each_cons(String.to_char_list("abacabaca"), 3)
['aba', 'bac', 'aca', 'cab', 'aba', 'bac', 'aca']
iex(3)> Cons.each_cons(String.to_char_list("abacabaca"), 2)
['ab', 'ba', 'ac', 'ca', 'ab', 'ba', 'ac', 'ca']
@dramsay
Copy link
Author

dramsay commented Sep 14, 2015

Great feedback - thanks! I should have caught the syntax issue with default parameters. Just getting started with Elixir but really liking it.

I would love to go to ElixirConf sometime, but unfortunately won't be making it this year. Hope to catch up with you soon, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment