Commit ddbd211a authored by Nicolas Lenz's avatar Nicolas Lenz
Browse files

Use pure functions instead of IO in Mock, refactoring

parent 0f8c0417
......@@ -14,16 +14,14 @@ main = do
case args of
[] -> putStrLn help
["--help"] -> putStrLn help
[style] -> getContents >>= (\input -> handle [style, input]) -- Read from stdin
_ -> handle args
[style] -> do
input <- getContents -- Read from stdin
putStrLn $ handle style [input]
(style:text) -> putStrLn $ handle style text
-- |Returns an IO action handling the given list of arguments.
handle :: [String] -> IO ()
handle (style:text) = transform (dropWhileEnd isSpace (intercalate " " text)) >>= putStrLn where
transform :: String -> IO String
transform = case lookup style styles of -- Lookup style name in styles list
Just f -> f -- Use the found style function
Nothing -> const $ return help -- If the style isn't found, always return just the help string
handle :: String -> [String] -> String
handle style = fromMaybe (const help) (lookup style styles) . dropWhileEnd isSpace . intercalate " "
-- |Help string.
help :: String
......
......@@ -4,7 +4,7 @@ cabal-version: 2.2
--
-- see: https://github.com/sol/hpack
--
-- hash: 82ed6980342b3ba1872fed0a8d1e6156efcc6d537a2a984fdda6414fba9d3a2b
-- hash: 386f26c4a9d9d6902416cd8ff7c1215a43e9a87f681dba72300c5060f7419567
name: mock
version: 2.0.0
......@@ -33,7 +33,9 @@ library
src
build-depends:
base >=4.7 && <5
, hashable
, random
, text
, time
default-language: Haskell2010
......@@ -46,7 +48,9 @@ executable mock
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, hashable
, mock
, random
, text
, time
default-language: Haskell2010
......@@ -16,7 +16,9 @@ description: Please see the README at <https://git.eisfunke.com/software
dependencies:
- base >= 4.7 && < 5
- random
- hashable
- time
- text
library:
source-dirs: src
......
......@@ -2,25 +2,22 @@ module Mock (styles, mockAlternate, mockRandom, letterspace, toDS) where
import Data.Char
import Data.List
import Data.Hashable
import System.Random
import Data.Time.Clock.POSIX
-- |List of possible mock style names and their functions.
styles :: [(String, String -> IO String)]
styles :: [(String, String -> String)]
styles = [
("random", mockRandom),
("alternate", toIO mockAlternate),
("space", toIO $ letterspace 1),
("space2", toIO $ letterspace 2),
("space3", toIO $ letterspace 3),
("upper", toIO $ map toUpper),
("lower", toIO $ map toLower),
("double", toIO $ map toDS)]
-- |Lifts a simple function into an IO operation simply returning what the function would return.
toIO :: (a -> b) -> (a -> IO b)
toIO f = \x -> return $ f x
("alternate", mockAlternate),
("space", letterspace 1),
("space2", letterspace 2),
("space3", letterspace 3),
("lines", intersperse '\n'),
("upper", map toUpper),
("lower", map toLower),
("double", map toDS)]
-- |Transforms a String into uppercase where the corresponding list is True. For False the String isn't changed.
toUpperBy :: String -> [Bool] -> String
......@@ -33,11 +30,9 @@ toUpperBy [] _ = []
mockAlternate :: String -> String
mockAlternate str = toUpperBy str $ intersperse True $ repeat False
-- |Tansforms random Chars of a String into uppercase.
mockRandom :: String -> IO String
mockRandom str = do
time <- fmap round getPOSIXTime
return $ toUpperBy str $ randoms $ mkStdGen time
-- |Tansforms random (that is, random per input String) Chars of a String into uppercase.
mockRandom :: String -> String
mockRandom str = toUpperBy str (randoms $ mkStdGen (hash str))
-- |Letterspaces a String with the given number of blanks between the Chars.
letterspace :: Int -> String -> String
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment