Skip to content

Commit f75ebc9

Browse files
committed
Update the Morse code application from ch14
1 parent 45ced5e commit f75ebc9

File tree

6 files changed

+95
-78
lines changed

6 files changed

+95
-78
lines changed

ch14/morse/LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright Author name here (c) 2016
1+
Copyright Dwayne Crooks (c) 2017
22

33
All rights reserved.
44

@@ -13,7 +13,7 @@ modification, are permitted provided that the following conditions are met:
1313
disclaimer in the documentation and/or other materials provided
1414
with the distribution.
1515

16-
* Neither the name of Author name here nor the names of other
16+
* Neither the name of Dwayne Crooks nor the names of other
1717
contributors may be used to endorse or promote products derived
1818
from this software without specific prior written permission.
1919

ch14/morse/src/Main.hs renamed to ch14/morse/app/Main.hs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@ module Main where
22

33
import Control.Monad (forever, when)
44
import Data.List (intercalate)
5-
-- import Data.Traversable (traverse)
5+
import Data.Traversable (traverse)
66
import Morse (stringToMorse, morseToChar)
77
import System.Environment (getArgs)
88
import System.Exit (exitFailure, exitSuccess)
99
import System.IO (hGetLine, hIsEOF, stdin)
1010

11+
12+
main :: IO ()
13+
main = do
14+
mode <- getArgs
15+
case mode of
16+
[arg] ->
17+
case arg of
18+
"from" -> convertFromMorse
19+
"to" -> convertToMorse
20+
_ -> argError
21+
_ -> argError
22+
where
23+
argError = do
24+
putStrLn "Please specify the\
25+
\ first argument\
26+
\ as being 'from' or\
27+
\ 'to' morse,\
28+
\ such as: morse to"
29+
exitFailure
30+
31+
1132
convertToMorse :: IO ()
1233
convertToMorse = forever $ do
1334
weAreDone <- hIsEOF stdin
@@ -20,11 +41,12 @@ convertToMorse = forever $ do
2041
convertLine line = do
2142
let morse = stringToMorse line
2243
case morse of
23-
(Just str) -> putStrLn $ intercalate " " str
44+
Just str -> putStrLn $ intercalate " " str
2445
Nothing -> do
2546
putStrLn $ "ERROR: " ++ line
2647
exitFailure
2748

49+
2850
convertFromMorse :: IO ()
2951
convertFromMorse = forever $ do
3052
weAreDone <- hIsEOF stdin
@@ -38,32 +60,7 @@ convertFromMorse = forever $ do
3860
let decoded :: Maybe String
3961
decoded = traverse morseToChar (words line)
4062
case decoded of
41-
(Just s) -> putStrLn s
63+
Just s -> putStrLn s
4264
Nothing -> do
4365
putStrLn $ "ERROR: " ++ line
4466
exitFailure
45-
46-
main :: IO ()
47-
main = do
48-
mode <- getArgs
49-
case mode of
50-
[arg] ->
51-
case arg of
52-
"from" -> convertFromMorse
53-
"to" -> convertToMorse
54-
_ -> argError
55-
_ -> argError
56-
57-
where
58-
argError = do
59-
putStrLn "Please specify the first argument as being \
60-
\'from' or 'to' morse, such as: morse to"
61-
exitFailure
62-
63-
-- Test cases
64-
-- echo "hi" | stack exec morse to
65-
-- .... ..
66-
-- echo ".... .." | stack exec morse from
67-
-- hi
68-
-- echo ".... . .-.. .-.. ---" | stack exec morse from
69-
-- hello

ch14/morse/morse.cabal

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
name: morse
22
version: 0.1.0.0
3-
synopsis: Morse code
4-
description: Please see README.md
5-
homepage: https://github.com/dwayne/morse
3+
-- synopsis:
4+
-- description:
5+
homepage: https://github.com/dwayne/morse#readme
66
license: BSD3
77
license-file: LICENSE
88
author: Dwayne Crooks
99
maintainer: me@dwaynecrooks.com
10-
copyright: 2016 Dwayne Crooks
10+
copyright: 2017 Dwayne Crooks
1111
category: Text
1212
build-type: Simple
13+
extra-source-files: README.md
1314
cabal-version: >=1.10
1415

15-
executable morse
16+
library
1617
hs-source-dirs: src
17-
main-is: Main.hs
18+
exposed-modules: Morse
1819
ghc-options: -Wall -fwarn-tabs
19-
default-language: Haskell2010
2020
build-depends: base >= 4.7 && < 5
2121
, containers
22-
, morse
2322
, QuickCheck
23+
default-language: Haskell2010
2424

25-
library
26-
hs-source-dirs: src
27-
exposed-modules: Morse
28-
ghc-options: -Wall -fwarn-tabs
29-
default-language: Haskell2010
30-
build-depends: base >= 4.7 && < 5
31-
, containers
32-
, QuickCheck
25+
executable morse
26+
hs-source-dirs: app
27+
main-is: Main.hs
28+
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -fwarn-tabs
29+
build-depends: base
30+
, containers
31+
, morse
32+
, QuickCheck
33+
default-language: Haskell2010
3334

3435
test-suite tests
35-
hs-source-dirs: tests
36-
ghc-options: -Wall -fno-warn-orphans
37-
type: exitcode-stdio-1.0
38-
main-is: tests.hs
39-
default-language: Haskell2010
40-
build-depends: base >= 4.7 && < 5
41-
, containers
42-
, morse
43-
, QuickCheck
36+
type: exitcode-stdio-1.0
37+
hs-source-dirs: tests
38+
main-is: tests.hs
39+
build-depends: base
40+
, containers
41+
, morse
42+
, QuickCheck
43+
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -fno-warn-orphans
44+
default-language: Haskell2010
45+
46+
source-repository head
47+
type: git
48+
location: https://github.com/dwayne/morse

ch14/morse/src/Morse.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ module Morse
99

1010
import qualified Data.Map as M
1111

12+
1213
type Morse = String
1314

14-
letterToMorse :: (M.Map Char Morse)
15+
16+
letterToMorse :: M.Map Char Morse
1517
letterToMorse = M.fromList
1618
[ ('a', ".-")
1719
, ('b', "-...")
@@ -51,14 +53,22 @@ letterToMorse = M.fromList
5153
, ('0', "-----")
5254
]
5355

56+
5457
morseToLetter :: M.Map Morse Char
55-
morseToLetter = M.foldWithKey (flip M.insert) M.empty letterToMorse
58+
morseToLetter =
59+
M.foldWithKey (flip M.insert) M.empty letterToMorse
60+
5661

5762
charToMorse :: Char -> Maybe Morse
58-
charToMorse c = M.lookup c letterToMorse
63+
charToMorse c =
64+
M.lookup c letterToMorse
65+
5966

6067
stringToMorse :: String -> Maybe [Morse]
61-
stringToMorse s = sequence $ fmap charToMorse s
68+
stringToMorse s =
69+
sequence $ fmap charToMorse s
70+
6271

6372
morseToChar :: Morse -> Maybe Char
64-
morseToChar m = M.lookup m morseToLetter
73+
morseToChar m =
74+
M.lookup m morseToLetter

ch14/morse/stack.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# This file was automatically generated by 'stack init'
2-
#
2+
#
33
# Some commonly used options have been documented as comments in this file.
44
# For advanced use and comprehensive documentation of the format, please see:
55
# http://docs.haskellstack.org/en/stable/yaml_configuration/
66

77
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
88
# A snapshot resolver dictates the compiler version and the set of packages
99
# to be used for project dependencies. For example:
10-
#
10+
#
1111
# resolver: lts-3.5
1212
# resolver: nightly-2015-09-21
1313
# resolver: ghc-7.10.2
1414
# resolver: ghcjs-0.1.0_ghc-7.10.2
1515
# resolver:
1616
# name: custom-snapshot
1717
# location: "./custom-snapshot.yaml"
18-
resolver: lts-5.15
18+
resolver: lts-8.2
1919

2020
# User packages to be built.
2121
# Various formats can be used as shown in the example below.
22-
#
22+
#
2323
# packages:
2424
# - some-directory
2525
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
@@ -31,7 +31,7 @@ resolver: lts-5.15
3131
# subdirs:
3232
# - auto-update
3333
# - wai
34-
#
34+
#
3535
# A package marked 'extra-dep: true' will only be built if demanded by a
3636
# non-dependency (i.e. a user package), and its test suites and benchmarks
3737
# will not be run. This is useful for tweaking upstream packages.
@@ -49,18 +49,18 @@ extra-package-dbs: []
4949

5050
# Control whether we use the GHC we find on the path
5151
# system-ghc: true
52-
#
52+
#
5353
# Require a specific version of stack, using version ranges
5454
# require-stack-version: -any # Default
55-
# require-stack-version: ">=1.1"
56-
#
55+
# require-stack-version: ">=1.3"
56+
#
5757
# Override the architecture used by stack, especially useful on Windows
5858
# arch: i386
5959
# arch: x86_64
60-
#
60+
#
6161
# Extra directories used by stack for building
6262
# extra-include-dirs: [/path/to/dir]
6363
# extra-lib-dirs: [/path/to/dir]
64-
#
64+
#
6565
# Allow a newer minor version of GHC than the snapshot specifies
6666
# compiler-check: newer-minor

ch14/morse/tests/tests.hs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ import qualified Data.Map as M
44
import Morse
55
import Test.QuickCheck
66

7+
8+
main :: IO ()
9+
main = quickCheck prop_thereAndBackAgain
10+
11+
12+
prop_thereAndBackAgain :: Property
13+
prop_thereAndBackAgain =
14+
forAll charGen (\c -> ((charToMorse c) >>= morseToChar) == Just c)
15+
16+
717
allowedChars :: [Char]
818
allowedChars = M.keys letterToMorse
919

20+
1021
allowedMorse :: [Morse]
1122
allowedMorse = M.elems letterToMorse
1223

24+
1325
charGen :: Gen Char
1426
charGen = elements allowedChars
1527

28+
1629
morseGen :: Gen Morse
1730
morseGen = elements allowedMorse
18-
19-
prop_thereAndBackAgain :: Property
20-
prop_thereAndBackAgain =
21-
forAll charGen
22-
(\c -> ((charToMorse c) >>= morseToChar) == Just c)
23-
24-
main :: IO ()
25-
main = quickCheck prop_thereAndBackAgain

0 commit comments

Comments
 (0)