Created
December 18, 2016 14:00
-
-
Save lpil/e04b4ac830c59a6f7eff7fc5af781691 to your computer and use it in GitHub Desktop.
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 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