Created
March 7, 2020 22:43
-
-
Save bettio/74a75f2fc0577b24f01663182910dce2 to your computer and use it in GitHub Desktop.
Elixir morse encoder example - step 4
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 MorseEncoder.Encoder do | |
def morse_encode(s) do | |
:string.to_upper(s) | |
|> Enum.map(&to_morse/1) | |
|> List.flatten() | |
end | |
defp to_morse(c) do | |
case c do | |
?\s -> ' ' | |
?0 -> '----- ' | |
?1 -> '.---- ' | |
?2 -> '..--- ' | |
?3 -> '...-- ' | |
?4 -> '....- ' | |
?5 -> '..... ' | |
?6 -> '-.... ' | |
?7 -> '--... ' | |
?8 -> '---.. ' | |
?9 -> '----. ' | |
?A -> '.- ' | |
?B -> '-... ' | |
?C -> '-.-. ' | |
?D -> '-.. ' | |
?E -> '. ' | |
?F -> '..-. ' | |
?G -> '--. ' | |
?H -> '.... ' | |
?I -> '.. ' | |
?J -> '.--- ' | |
?K -> '-.- ' | |
?L -> '.-.. ' | |
?M -> '-- ' | |
?N -> '-. ' | |
?O -> '--- ' | |
?P -> '.--. ' | |
?Q -> '--.- ' | |
?R -> '.-. ' | |
?S -> '... ' | |
?T -> '- ' | |
?U -> '..- ' | |
?V -> '...- ' | |
?W -> '.-- ' | |
?X -> '-..- ' | |
?Y -> '-.-- ' | |
?Z -> '--.. ' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment