Last active
September 14, 2015 12:36
-
-
Save dramsay/cf1f0fd425b1d9b376ae to your computer and use it in GitHub Desktop.
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
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'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.