Skip to content

Instantly share code, notes, and snippets.

@tonywok
Created April 7, 2017 17:41

Revisions

  1. tonywok created this gist Apr 7, 2017.
    17 changes: 17 additions & 0 deletions acronym.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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