Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Software
Mock
Commits
10610588
Commit
10610588
authored
Nov 23, 2018
by
Nicolas Lenz
Browse files
Refactoring and documentation
parent
9d58504d
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/Main.hs
View file @
10610588
...
...
@@ -7,22 +7,25 @@ import Data.Maybe
import
System.Environment
-- |Main function.
main
::
IO
()
main
=
do
args
<-
getArgs
case
args
of
[]
->
putStrLn
help
[
"--help"
]
->
putStrLn
help
[
_
]
->
getContents
>>=
(
\
tex
t
->
handle
[
head
args
,
text
])
[
style
]
->
getContents
>>=
(
\
inpu
t
->
handle
[
style
,
input
])
-- Read from stdin
_
->
handle
args
-- |Returns an IO action handling the given list of arguments.
handle
::
[
String
]
->
IO
()
handle
(
style
:
text
)
=
transform
(
intercalate
" "
text
)
>>=
putStrLn
where
transform
::
String
->
IO
String
transform
=
case
lookup
style
styles
of
Just
f
->
f
Nothing
->
const
$
return
help
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
-- |List of possible mock styles names and their functions.
styles
::
[(
String
,
String
->
IO
String
)]
styles
=
[
(
"random"
,
mockRandom
),
...
...
@@ -31,13 +34,15 @@ styles = [
(
"space2"
,
toIO
$
mockSpace
2
),
(
"space3"
,
toIO
$
mockSpace
3
),
(
"upper"
,
toIO
$
map
toUpper
),
(
"lower"
,
toIO
$
map
toLower
)
]
(
"lower"
,
toIO
$
map
toLower
)]
-- |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
)
toIO
f
=
\
x
->
return
$
f
x
-- |Help string.
help
::
String
help
=
"Mock - a program to transform text.
\n\n\
help
=
"Mock - a program to transform text.
\n\
\\n\
\
Usage: mock [STYLE] [TEXT]
\n\
\
Styles: "
++
(
intercalate
", "
$
map
fst
styles
)
src/Mock.hs
View file @
10610588
...
...
@@ -6,19 +6,23 @@ import System.Random
import
Data.Time.Clock.POSIX
-- |Transforms a String into uppercase where the corresponding list is True. For False the String isn't changed.
toUpperBy
::
String
->
[
Bool
]
->
String
toUpperBy
(
c
:
cs
)
(
True
:
bs
)
=
toUpper
c
:
toUpperBy
cs
bs
toUpperBy
(
c
:
cs
)
(
False
:
bs
)
=
c
:
toUpperBy
cs
bs
toUpperBy
(
cs
)
[]
=
cs
toUpperBy
[]
_
=
[]
-- |Transforms every other of the Chars of a String into uppercase. The other Chars aren't changed.
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
-- |Letterspaces a String with the given number of blanks between the Chars.
mockSpace
::
Int
->
String
->
String
mockSpace
n
=
intercalate
(
replicate
n
' '
)
.
map
(
\
c
->
[
c
])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment