RosettaCodeData/Task/Morse-code/Elixir/morse-code.ex

26 lines
1.4 KiB
Elixir

defmodule Morse do
@morse %{"!" => "---.", "\"" => ".-..-.", "$" => "...-..-", "'" => ".----.",
"(" => "-.--.", ")" => "-.--.-", "+" => ".-.-.", "," => "--..--",
"-" => "-....-", "." => ".-.-.-", "/" => "-..-.",
"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" => "--..",
"[" => "-.--.", "]" => "-.--.-", "_" => "..--.-" }
def code(text) do
String.upcase(text)
|> String.codepoints
|> Enum.map_join(" ", fn c -> Map.get(@morse, c, " ") end)
end
end
IO.puts Morse.code("Hello, World!")