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
67
68
69
70
71
72
73
74
75
|
{-# LANGUAGE OverloadedStrings #-}
module Mail
( mailSubject
, mailBody
) where
import Data.Text (Text)
import qualified Data.Text as T
import Date
import Birthdate
mailSubject :: [Birthdate] -> Text
mailSubject birthdates =
let count = length birthdates
in T.concat
[ "Hey, "
, if count > 1 then "there are" else "there is"
, " "
, T.pack . show $ count
, " birthday"
, if count > 1 then "s" else ""
, " today!"
]
mailBody :: Date -> [Birthdate] -> Text
mailBody currentDate birthdates =
let count = length birthdates
birthdatesWithLines = map (mapFst getLine) . zip [1..] $ birthdates
getLine 1 = if count == 1 then SingleLine else FirstLine
getLine line = if line == count then LastLine else MiddleLine
in T.concat $ map (mailLine currentDate) birthdatesWithLines
mapFst :: (a -> c) -> (a, b) -> (c, b)
mapFst f (x, y) = (f x, y)
data Line =
SingleLine
| FirstLine
| MiddleLine
| LastLine
deriving (Eq, Show)
mailLine :: Date -> (Line, Birthdate) -> Text
mailLine date (SingleLine, birthdate) =
T.concat
[ fullname birthdate
, " is now "
, T.pack . show $ age date birthdate
, " years old."
]
mailLine date (FirstLine, birthdate) =
T.concat
[ fullname birthdate
, " is now "
, T.pack . show $ age date birthdate
, " years old"
]
mailLine date (MiddleLine, birthdate) =
T.concat
[ ", "
, fullname birthdate
, " is "
, T.pack . show $ age date birthdate
, " years old"
]
mailLine date (LastLine, birthdate) =
T.concat
[ " and "
, fullname birthdate
, " is "
, T.pack . show $ age date birthdate
, " years old."
]
|