Last active
December 17, 2015 11:13
-
-
Save jagira/cfeec5562bf55090efdf to your computer and use it in GitHub Desktop.
Generate a list of ranges from a list of characters
This file contains hidden or 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 ListToRange do | |
def generate(list) do | |
generate(list, []) | |
end | |
def generate([_], accumulator) do | |
accumulator | |
end | |
def generate([head | tail], accumulator) do | |
[next | _] = tail | |
generate(tail, accumulator ++ ["#{head} - #{next}"]) | |
end | |
end | |
# ListToRange.generate([0, 10, 20, 30]) -> ["0 - 10", "10 - 20", "20 -30"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment