Created
April 7, 2017 17:41
-
-
Save tonywok/8b6db8f154d643af7fc0bd951bc3560a to your computer and use it in GitHub Desktop.
acronym.exs
This file contains hidden or 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
module Acronym | |
def abbreviate(string) do | |
string | |
|> String.codepoints | |
|> Enum.with_index | |
|> Enum.reduce("", fn({letter, idx}, acronym) -> | |
cond do | |
String.upcase(letter) == letter and String.downcase(letter) != letter -> | |
acronym <> letter | |
String.at(string, idx-1) == " " -> | |
acronym <> String.upcase(letter) | |
true -> | |
acronym | |
end | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment