Skip to content

Instantly share code, notes, and snippets.

@bettio
Created March 7, 2020 22:43
Show Gist options
  • Save bettio/74a75f2fc0577b24f01663182910dce2 to your computer and use it in GitHub Desktop.
Save bettio/74a75f2fc0577b24f01663182910dce2 to your computer and use it in GitHub Desktop.
Elixir morse encoder example - step 4
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