-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPets.hs
66 lines (50 loc) · 1.76 KB
/
Pets.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Pets where
import Control.Applicative (pure)
import Control.Category ((<<<))
import Control.Monad (void, (>>))
import Data.Bool
import Data.Foldable (foldMap)
import Data.Function (($))
import Data.Functor ((<$>), (<&>))
import Data.List (filter, intersperse)
import Data.Monoid (Sum (..), mconcat, (<>))
import Data.Text (Text)
import Data.Traversable (traverse)
import Data.Int (Int)
import GHC.Show (Show (show))
import System.IO (FilePath, Handle, IO, IOMode (..), hPutStrLn, print , withFile)
data Pet = MkPet { petName :: Text, petAnimal :: Animal } deriving (Show)
data Animal = Cat | Dog deriving (Show)
mkCat name = MkPet name Cat
mkDog name = MkPet name Dog
pets =
[ mkCat "garfield"
, mkDog "odie"
, mkCat "nermal"
]
isCat (MkPet _ Cat) = True
isCat _ = False
isDog (MkPet _ Dog) = True
isDog _ = False
cats = filter isCat pets
dogs = filter isDog pets
catNames = cats <&> petName
dogNames = petName <$> dogs
namesString = mconcat <<< intersperse ", "
catNamesString = namesString catNames
dogNamesString = namesString dogNames
writePet :: Handle -> Pet -> IO Pet
writePet h pet@(MkPet name animal) = hPutStrLn h textRep >> pure pet
where textRep = show animal <> "{ " <> show name <> "}"
writeCats :: FilePath -> [Pet] -> IO ()
writeCats path xs = withFile path WriteMode (\h -> void $ traverse (writePet h) xs)
writeDogs :: FilePath -> [Pet] -> IO ()
writeDogs path xs = withFile path WriteMode (\h -> void $ traverse (writePet h) xs)
scorePet (MkPet _ Cat) = Sum 1
scorePet (MkPet _ Dog) = Sum 2
example :: Sum Int
example = foldMap scorePet pets
main = do
writeCats "/tmp/cats.txt" cats
writeDogs "/tmp/dogs.txt" dogs
print example