aboutsummaryrefslogtreecommitdiff
path: root/ch09/Module_9_b_3.hs
blob: ce387177ab44a196654e23bcea4961cac1dd8ef2 (plain)
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
-- Take the predicates and combinators from "Gluing predicates together" on page
-- 224 and make them work with our new Info type.

module Module_9_b_3 where

{-- From examples/examples/ch09/BetterPredicate.hs modified according to the assignment --}
import Data.Time (UTCTime(..))
import System.Directory (Permissions(..))
import System.FilePath (takeExtension)

import ControlledVisit (Info(..))


type InfoF a = Info -> a


equalP :: (Eq a) => InfoF a -> a -> (InfoF Bool)
equalP f k = (\info -> f info == k)

equalP' :: (Eq a) => (InfoF a) -> a -> (InfoF Bool)
equalP' f k info = (f info == k)

liftP :: (a -> b -> Bool) -> (InfoF a) -> b -> (InfoF Bool)
liftP q f k info = f info `q` k

greaterP, lesserP :: (Ord a) => (InfoF a) -> a -> (InfoF Bool)
greaterP = liftP (>)
lesserP = liftP (<)

simpleAndP :: (InfoF Bool) -> (InfoF Bool) -> (InfoF Bool)
simpleAndP f g info = f info && g info

liftP2 :: (a -> b -> Bool) -> (InfoF a) -> (InfoF b) -> (InfoF Bool)
liftP2 q f g info = f info `q` g info

andP = liftP2 (&&)
orP = liftP2 (||)

constP :: a -> (InfoF a)
constP k _ = k

liftP' q f k info = f info `q` constP k info

liftPath :: (FilePath -> a) -> (InfoF a)
liftPath f info = f (infoPath info)

myTest2 = (liftPath takeExtension `equalP` ".cpp") `andP`
          (infoSize `greaterP` (Just 131072))
{-- End of code from examples --}

-- ghci> :l Module_9_b_3.hs
-- [1 of 2] Compiling ControlledVisit  ( ControlledVisit.hs, interpreted )
-- [2 of 2] Compiling Module_9_b_3     ( Module_9_b_3.hs, interpreted )
-- Ok, two modules loaded.

-- ghci> infoA = Info "asdf.cpp" Nothing (Just 131072) Nothing
-- ghci> myTest2 infoA
-- False

-- ghci> infoB = Info "asdf.hs" Nothing (Just 131073) Nothing
-- ghci> myTest2 infoB
-- False

-- ghci> infoC = Info "asdf.cpp" Nothing (Just 131073) Nothing
-- ghci> myTest2 infoC
-- True