/
BasicPrelude.hs
165 lines (137 loc) · 3.49 KB
/
BasicPrelude.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{-# LANGUAGE NoImplicitPrelude #-}
-- | BasicPrelude mostly re-exports
-- several key libraries in their entirety.
-- The exception is Data.List,
-- where various functions are replaced
-- by similar versions that are either
-- generalized, operate on Text,
-- or are implemented strictly.
module BasicPrelude
( -- * Module exports
module CorePrelude
, module Data.List
, module Control.Monad
-- * Enhanced exports
-- ** Simpler name for a typeclassed operation
, map
, empty
, (++)
, concat
-- ** Strict implementation
, sum
, product
-- ** Text for Read and Show operations
, show
, read
, readIO
-- ** FilePath for file operations
, readFile
, writeFile
, appendFile
-- * Text exports
-- ** Text operations (Pure)
, Text.lines
, Text.words
, Text.unlines
, Text.unwords
, Text.intercalate
-- ** Text operations (IO)
, Text.putStr
, Text.getLine
, LText.getContents
, LText.interact
-- * Miscellaneous prelude re-exports
-- ** Math
, Prelude.gcd
, Prelude.lcm
-- ** Show and Read
, Prelude.String
, Prelude.ShowS
, Prelude.showsPrec
, Prelude.showList
, Prelude.shows
, Prelude.showChar
, Prelude.showString
, Prelude.showParen
, Prelude.ReadS
, Prelude.readsPrec
, Prelude.readList
, Prelude.reads
, Prelude.readParen
, Prelude.lex
-- ** IO operations
, Prelude.putChar
, Prelude.getChar
, Prelude.readLn
-- ** Exceptions
, Prelude.IOError
, Prelude.ioError
, Prelude.userError
) where
import CorePrelude
import Data.List hiding
( -- prefer monoid versions instead
(++)
, concat
-- prefer Text versions instead
, lines
, words
, unlines
, unwords
, intercalate
-- prefer map = fmap instead
, map
-- prefer strict versions
, sum
, product
)
-- Import *all of the things* from Control.Monad,
-- specifically, the list-based things that
-- CorePrelude doesn't export
import Control.Monad
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import qualified Data.Text.Lazy.IO as LText
import qualified Filesystem.Path.CurrentOS as FilePath
import qualified Prelude
-- | > map = fmap
map :: (Functor f) => (a -> b) -> f a -> f b
map = fmap
-- | > empty = mempty
empty :: Monoid w => w
empty = mempty
infixr 5 ++
-- | > (++) = mappend
(++) :: Monoid w => w -> w -> w
(++) = mappend
-- | > concat = mconcat
concat :: Monoid w => [w] -> w
concat = mconcat
-- | Compute the sum of a finite list of numbers.
sum :: Num a => [a] -> a
sum = foldl' (+) 0
-- | Compute the product of a finite list of numbers.
product :: Num a => [a] -> a
product = foldl' (*) 1
-- | Convert a value to readable Text
show :: Show a => a -> Text
show = Text.pack . Prelude.show
-- | Parse Text to a value
read :: Read a => Text -> a
read = Prelude.read . Text.unpack
-- | The readIO function is similar to read
-- except that it signals parse failure to the IO monad
-- instead of terminating the program.
readIO :: Read a => Text -> IO a
readIO = Prelude.readIO . Text.unpack
-- | Read a file and return the contents of the file as Text.
-- The entire file is read strictly.
readFile :: FilePath -> IO Text
readFile = Text.readFile . FilePath.encodeString
-- | Write Text to a file.
-- The file is truncated to zero length before writing begins.
writeFile :: FilePath -> Text -> IO ()
writeFile = Text.writeFile . FilePath.encodeString
-- | Write Text to the end of a file.
appendFile :: FilePath -> Text -> IO ()
appendFile = Text.appendFile . FilePath.encodeString