Skip to content

Instantly share code, notes, and snippets.

@Maartz
Last active May 19, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maartz/6ed6a940131909d72cd8e46794ff1131 to your computer and use it in GitHub Desktop.
Save Maartz/6ed6a940131909d72cd8e46794ff1131 to your computer and use it in GitHub Desktop.
defmodule DigitalNumbers do
@moduledoc """
DigitalNumbers module provides an API to convert integer to LCD styled string integer
"""
@digital_numbers %{
0 => """
_
| |
|_|
""",
1 => """
|
|
""",
2 => """
_
_|
|_
""",
3 => """
_
_|
_|
""",
4 => """
|_|
|
""",
5 => """
_
|_
_|
""",
6 => """
_
|_
|_|
""",
7 => """
_
|
|
""",
8 => """
_
|_|
|_|
""",
9 => """
_
|_|
|
""",
}
@doc """
Print in the console the input in a LCD clock fashion.
"""
def print(input \\ 0) do
input
|> parse_and_split()
|> Stream.map(&String.to_integer/1)
|> Stream.map(&transform_input_to_digital/1)
|> Enum.reduce([], fn number, acc -> [number | acc] end)
|> Enum.reverse()
end
@doc ~S"""
Parse the input as the key and get it from the Module attribute printing a string formated value.
## Examples
iex> DigitalNumbers.transform_input_to_digital(2)
" _\n _|\n|_\n"
iex> DigitalNumbers.transform_input_to_digital("1221")
"Feed me with number only..."
iex> DigitalNumbers.transform_input_to_digital(:ok)
"Feed me with number only..."
"""
def transform_input_to_digital(input) when is_integer(input) and input >= 0 and input <= 9 do
Map.get(@digital_numbers, input)
end
def transform_input_to_digital(_) do
"Feed me with number only..."
end
@doc """
Convert the number based input in list of string(s).
"""
defp parse_and_split(input) do
input
|> Integer.to_string()
|> String.split("", trim: true)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment