Skip to content
Newer
Older
100644 121 lines (86 sloc) 3.15 KB
14e9771 @serras Initial commit
authored Dec 8, 2010
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : AFRPMiscellany
4 -- Copyright : (c) Yale University, 2003
5 -- License : BSD-style (see the file LICENSE)
6 --
7 -- Maintainer : antony@apocalypse.org
8 -- Stability : provisional
9 -- Portability : non-portable (uses GHC extensions)
10 --
11 -- Collection of entities that really should be part
12 -- the Haskell 98 prelude or simply have no better
13 -- home.
14 --
15 module AFRPMiscellany (
16 -- Reverse function composition
17 ( # ), -- :: (a -> b) -> (b -> c) -> (a -> c), infixl 9
18
19 -- Arrow plumbing aids
20 dup, -- :: a -> (a,a)
21 swap, -- :: (a,b) -> (b,a)
22
23 -- Maps over lists of pairs
24 mapFst, -- :: (a -> b) -> [(a,c)] -> [(b,c)]
25 mapSnd, -- :: (a -> b) -> [(c,a)] -> [(c,b)]
26
27 -- Generalized tuple selectors
28 sel3_1, sel3_2, sel3_3,
29 sel4_1, sel4_2, sel4_3, sel4_4,
30 sel5_1, sel5_2, sel5_3, sel5_4, sel5_5,
31
32 -- Floating point utilities
33 fDiv, -- :: (RealFrac a, Integral b) => a -> a -> b
34 fMod, -- :: RealFrac a => a -> a -> a
35 fDivMod -- :: (RealFrac a, Integral b) => a -> a -> (b, a)
36 ) where
37
38 infixl 9 #
39 infixl 7 `fDiv`, `fMod`
40
41
42 ------------------------------------------------------------------------------
43 -- Reverse function composition
44 ------------------------------------------------------------------------------
45
46 ( # ) :: (a -> b) -> (b -> c) -> (a -> c)
47 f # g = g . f
48
49
50 ------------------------------------------------------------------------------
51 -- Arrow plumbing aids
52 ------------------------------------------------------------------------------
53
54 dup :: a -> (a,a)
55 dup x = (x,x)
56
57 swap :: (a,b) -> (b,a)
58 swap ~(x,y) = (y,x)
59
60
61 ------------------------------------------------------------------------------
62 -- Maps over lists of pairs
63 ------------------------------------------------------------------------------
64
65 mapFst :: (a -> b) -> [(a,c)] -> [(b,c)]
66 mapFst f [] = []
67 mapFst f ((x, y) : xys) = (f x, y) : mapFst f xys
68
69 mapSnd :: (a -> b) -> [(c,a)] -> [(c,b)]
70 mapSnd f [] = []
71 mapSnd f ((x, y) : xys) = (x, f y) : mapSnd f xys
72
73
74 ------------------------------------------------------------------------------
75 -- Generalized tuple selectors
76 ------------------------------------------------------------------------------
77
78 -- Triples
79
80 sel3_1 (x,_,_) = x
81 sel3_2 (_,x,_) = x
82 sel3_3 (_,_,x) = x
83
84
85 -- 4-tuples
86
87 sel4_1 (x,_,_,_) = x
88 sel4_2 (_,x,_,_) = x
89 sel4_3 (_,_,x,_) = x
90 sel4_4 (_,_,_,x) = x
91
92
93 -- 5-tuples
94
95 sel5_1 (x,_,_,_,_) = x
96 sel5_2 (_,x,_,_,_) = x
97 sel5_3 (_,_,x,_,_) = x
98 sel5_4 (_,_,_,x,_) = x
99 sel5_5 (_,_,_,_,x) = x
100
101
102 ------------------------------------------------------------------------------
103 -- Floating point utilities
104 ------------------------------------------------------------------------------
105
106 -- Floating-point div and modulo operators.
107
108 fDiv :: (RealFrac a, Integral b) => a -> a -> b
109 fDiv x y = fst (fDivMod x y)
110
111
112 fMod :: RealFrac a => a -> a -> a
113 fMod x y = snd (fDivMod x y)
114
115
116 fDivMod :: (RealFrac a, Integral b) => a -> a -> (b, a)
117 fDivMod x y = (q, r)
118 where
119 q = (floor (x/y))
120 r = x - fromIntegral q * y
Something went wrong with that request. Please try again.