Skip to content

Instantly share code, notes, and snippets.

@lpil
Created December 18, 2016 14:00
Show Gist options
  • Save lpil/e04b4ac830c59a6f7eff7fc5af781691 to your computer and use it in GitHub Desktop.
Save lpil/e04b4ac830c59a6f7eff7fc5af781691 to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Control.Monad.Eff.Console
import Data.Foldable (foldl)
import Data.String as String
import Data.Char as Char
import Data.List as List
import Data.List (List(Nil), (:))
import Data.Array as Array
import TryPureScript
abbreviate :: String -> String
abbreviate =
initials <<< List.fromFoldable <<< String.toCharArray
data CharType
= Upper
| Lower
| Space
| Other
classify :: Char -> CharType
classify c
| 'A' <= c && c <= 'Z' = Upper
| 'a' <= c && c <= 'z' = Lower
| c == ' ' || c == '-' = Space
| otherwise = Other
initials :: List Char -> String
initials =
_.acc <<< foldl step { acc: "", last: Space }
where
next Lower Upper c = String.singleton c
next Space _ c = String.singleton (Char.toUpper c)
next _ _ _ = ""
step { acc, last } c =
let this = classify c in { acc: acc <> next last this c, last: this }
main = render =<< withConsole do
logShow (abbreviate "Ruby on Rails")
logShow (abbreviate "Portable Networks Graphic")
logShow (abbreviate "HyperText Markup Language")
logShow (abbreviate "First in, First out")
logShow (abbreviate "Complementary Metal-Oxide semiconductor")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment