Skip to content

Instantly share code, notes, and snippets.

@jagira
Last active December 17, 2015 11:13
Show Gist options
  • Save jagira/cfeec5562bf55090efdf to your computer and use it in GitHub Desktop.
Save jagira/cfeec5562bf55090efdf to your computer and use it in GitHub Desktop.
Generate a list of ranges from a list of 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