Created
September 20, 2016 11:41
-
-
Save YusukeHosonuma/f1d49fb0df79976262182333528f2f24 to your computer and use it in GitHub Desktop.
Array Literal Generator
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
| #!/usr/bin/env stack | |
| -- stack --resolver lts-7.0 --install-ghc runghc --package turtle | |
| {-# LANGUAGE OverloadedStrings #-} | |
| import Data.Text (pack, unpack) | |
| import Turtle | |
| joinM :: (Monoid m) => m -> [m] -> m | |
| joinM separator = foldl1 (\a b -> a `mappend` separator `mappend` b) | |
| wrap :: Char -> String -> String | |
| wrap c = wrap' c c | |
| wrap' :: Char -> Char -> String -> String | |
| wrap' c1 c2 s = [c1] ++ s ++ [c2] | |
| parser :: Parser (Maybe Text) | |
| parser = optional (optText "lang" 'l' "Language (swift|objc|java) - default is swift") | |
| toArrayLiteral :: Text -> [String] -> String | |
| toArrayLiteral "swift" = wrap' '[' ']' . joinM ", " . map (wrap '"') | |
| toArrayLiteral "objc" = ('@':) . wrap' '[' ']' . joinM ", " . map ('@':) . map (wrap '"') | |
| toArrayLiteral "java" = swiftArray | |
| toArrayLiteral lang = \_ -> unpack $ "Not supported language type '" <> lang <> "'" | |
| main = do | |
| mLang <- options "Array Literal Converter" parser | |
| contents <- getContents | |
| let lang = case mLang of Nothing -> "swift" | |
| Just l -> l | |
| sink = toArrayLiteral lang | |
| let result = sink . lines $ contents | |
| putStrLn result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment