evantianx / CodeWars-Haskell

0 stars 0 forks source link

Convert string to camel case #7

Open evantianx opened 5 years ago

evantianx commented 5 years ago

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.

Examples

toCamelCase "the-stealth-warrior" -- returns "theStealthWarrior"

toCamelCase "The_Stealth_Warrior" -- returns "TheStealthWarrior"
evantianx commented 5 years ago

Solutions

import Data.List.Split
import Data.Char

capitalize :: String -> String
capitalize []  = []
capitalize (x:xs) = toUpper x : xs

toCamelCase :: String -> String
toCamelCase str = concat $ head words : map capitalize (tail words)
  where words = splitOneOf "-_" str