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 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