DEV Community

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
 
pdandy profile image
Andy Thompson

Came up with this Elm solution while I was procrastinating at work today:

capitalizeFirstLast : String -> String
capitalizeFirstLast input =
  let
    capitalizeFirst = String.left 1 >> String.toUpper
    capitalizeLast  = String.right 1 >> String.toUpper
    lowercaseMiddle = String.slice 1 -1 >> String.toLower
    transform word  =
      if String.length word < 2 then
        String.toUpper word
      else
        capitalizeFirst word ++ lowercaseMiddle word ++ capitalizeLast word
  in
  String.words input
    |> List.map transform
    |> String.join " "

Not overly different from the other solutions here, but neat nonetheless.