File: Baseball.hs

package info (click to toggle)
haskell-hxt 9.3.1.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: haskell: 15,853; xml: 1,464; makefile: 390
file content (320 lines) | stat: -rw-r--r-- 11,150 bytes parent folder | download | duplicates (7)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
{- |
  Example for usage of pickler functions
  to de-/serialise from/to XML

  Example data is taken from haskell wiki
  http://www.haskell.org/haskellwiki/HXT/Practical/Simple2
-}

module Main
where

import           Data.Map (Map, fromList, toList)

import Text.XML.HXT.Core

-- Example data taken from:
-- http://www.ibiblio.org/xml/books/bible/examples/05/5-1.xml

-- ------------------------------------------------------------
-- the data modell

data Season = Season
    { sYear    :: Int
    , sLeagues :: Leagues
    }
              deriving (Show, Eq)

type Leagues   = Map String Divisions

type Divisions = Map String [Team]

data Team = Team
    { teamName :: String
    , city     :: String
    , players  :: [Player]
    }
            deriving (Show, Eq)
             
data Player = Player
    { firstName :: String
    , lastName  :: String
    , position  :: String
    , atBats    :: Maybe Int
    , hits      :: Maybe Int
    , era       :: Maybe Float
    }
              deriving (Show, Eq)

-- ------------------------------------------------------------
-- the pickler instance declarations
-- in this case just for uniform naming

instance XmlPickler Season where
    xpickle = xpSeason

instance XmlPickler Team where
    xpickle = xpTeam

instance XmlPickler Player where
    xpickle = xpPlayer

-- ------------------------------------------------------------
-- for every data type there is a pickler

-- the XML root element

xpSeason        :: PU Season
xpSeason
    = xpElem "SEASON" $
      xpWrap ( uncurry Season
             , \ s -> (sYear s, sLeagues s)) $
      xpPair (xpAttr "YEAR" xpickle) xpLeagues

xpLeagues       :: PU Leagues
xpLeagues
    = xpWrap ( fromList
             , toList ) $
      xpList $
      xpElem "LEAGUE" $
      xpPair (xpAttr "NAME" xpText) xpDivisions

xpDivisions     :: PU Divisions
xpDivisions
    = xpWrap ( fromList
             , toList
             ) $
      xpList $
      xpElem "DIVISION" $
      xpPair (xpAttr "NAME" xpText)
             xpickle

xpTeam  :: PU Team
xpTeam
    = xpElem "TEAM" $
      xpWrap ( uncurry3 Team
             , \ t -> (teamName t, city t, players t)
             ) $
      xpTriple (xpAttr "NAME" xpText)
               (xpAttr "CITY" xpText)
               (xpList xpickle)

xpPlayer        :: PU Player
xpPlayer
    = xpElem "PLAYER" $
      xpWrap ( \ ((f,l,p,a,h,e)) -> Player f l p a h e
             , \ t -> (firstName t, lastName t
                      , position t, atBats t
                      , hits t, era t
                      )
             ) $
      xp6Tuple (xpAttr "GIVEN_NAME" xpText)
               (xpAttr "SURNAME"    xpText)
               (xpAttr "POSITION"   xpText)
               (xpOption (xpAttr "AT_BATS" xpickle))
               (xpOption (xpAttr "HITS"    xpickle))
               (xpOption (xpAttr "ERA"     xpPrim ))


-- ------------------------------------------------------------

-- a simple pickle/unpickle application

main    :: IO ()
main
    = do
      runX ( xunpickleDocument xpSeason [ withValidate no
                                        , withTrace 1
                                        , withRemoveWS yes
                                        , withPreserveComment no
                                        ] "simple2.xml"
             >>>
             processSeason
             >>>
             xpickleDocument xpSeason [ withIndent yes
                                      ] "new-simple2.xml"
           )
      return ()

-- the dummy for processing the unpickled data

processSeason   :: IOSArrow Season Season
processSeason
    = arrIO ( \ x -> do {print x ; return x})

-- ------------------------------------------------------------

-- the internal data of "simple2.xml"

season1998      :: Season
season1998
    = Season
      { sYear = 1998
      , sLeagues = fromList
        [ ( "American League"
          , fromList
            [ ( "Central"
              , [ Team { teamName = "White Sox"
                       , city = "Chicago"
                       , players = []}
                , Team { teamName = "Royals"
                       , city = "Kansas City"
                       , players = []}
                , Team { teamName = "Tigers"
                       , city = "Detroit"
                       , players = []}
                , Team { teamName = "Indians"
                       , city = "Cleveland"
                       , players = []}
                , Team { teamName = "Twins"
                       , city = "Minnesota"
                       , players = []}
                ])
            , ( "East"
              , [ Team { teamName = "Orioles"
                       , city = "Baltimore"
                       , players = []}
                , Team { teamName = "Red Sox"
                       , city = "Boston"
                       , players = []}
                , Team { teamName = "Yankees"
                       , city = "New York"
                       , players = []}
                , Team { teamName = "Devil Rays"
                       , city = "Tampa Bay"
                       , players = []}
                , Team { teamName = "Blue Jays"
                       , city = "Toronto"
                       , players = []}
                ])
            , ( "West"
              , [ Team { teamName = "Angels"
                       , city = "Anaheim"
                       , players = []}
                , Team { teamName = "Athletics"
                       , city = "Oakland"
                       , players = []}
                , Team { teamName = "Mariners"
                       , city = "Seattle"
                       , players = []}
                , Team { teamName = "Rangers"
                       , city = "Texas"
                       , players = []}
                ])
            ])
        , ( "National League"
          , fromList
            [ ( "Central"
              , [ Team { teamName = "Cubs"
                       , city = "Chicago"
                       , players = []}
                , Team { teamName = "Reds"
                       , city = "Cincinnati"
                       , players = []}
                , Team { teamName = "Astros"
                       , city = "Houston"
                       , players = []}
                , Team { teamName = "Brewers"
                       , city = "Milwaukee"
                       , players = []}
                , Team { teamName = "Pirates"
                       , city = "Pittsburgh"
                       , players = []}
                , Team { teamName = "Cardinals"
                       , city = "St. Louis"
                       , players = []}
                ])
            , ( "East"
              , [ Team { teamName = "Braves"
                       , city = "Atlanta"
                       , players =
                         [ Player { firstName = "Marty"
                                  , lastName = "Malloy"
                                  , position = "Second Base"
                                  , atBats = Just 28
                                  , hits = Just 5
                                  , era = Nothing}
                         , Player { firstName = "Ozzie"
                                  , lastName = "Guillen"
                                  , position = "Shortstop"
                                  , atBats = Just 264
                                  , hits = Just 73
                                  , era = Nothing}
                         , Player { firstName = "Danny"
                                  , lastName = "Bautista"
                                  , position = "Outfield"
                                  , atBats = Just 144
                                  , hits = Just 36
                                  , era = Nothing}
                         , Player { firstName = "Gerald"
                                  , lastName = "Williams"
                                  , position = "Outfield"
                                  , atBats = Just 266
                                  , hits = Just 81
                                  , era = Nothing}
                         , Player { firstName = "Tom"
                                  , lastName = "Glavine"
                                  , position = "Starting Pitcher"
                                  , atBats = Nothing
                                  , hits = Nothing
                                  , era = Just 2.47}
                         , Player { firstName = "Javier"
                                  , lastName = "Lopez"
                                  , position = "Catcher"
                                  , atBats = Just 489
                                  , hits = Just 139
                                  , era = Nothing}
                         , Player { firstName = "Ryan"
                                  , lastName = "Klesko"
                                  , position = "Outfield"
                                  , atBats = Just 427
                                  , hits = Just 117
                                  , era = Nothing}
                         , Player { firstName = "Andres"
                                  , lastName = "Galarraga"
                                  , position = "First Base"
                                  , atBats = Just 555
                                  , hits = Just 169
                                  , era = Nothing}
                         , Player { firstName = "Wes"
                                  , lastName = "Helms"
                                  , position = "Third Base"
                                  , atBats = Just 13
                                  , hits = Just 4
                                  , era = Nothing}
                         ]}
                , Team { teamName = "Marlins"
                       , city = "Florida"
                       , players = []}
                , Team { teamName = "Expos"
                       , city = "Montreal"
                       , players = []}
                , Team { teamName = "Mets"
                       , city = "New York"
                       , players = []}
                , Team { teamName = "Phillies"
                       , city = "Philadelphia"
                       , players = []}
                ])
            , ( "West"
              , [ Team { teamName = "Diamondbacks"
                       , city = "Arizona"
                       , players = []}
                , Team { teamName = "Rockies"
                       , city = "Colorado"
                       , players = []}
                , Team { teamName = "Dodgers"
                       , city = "Los Angeles"
                       , players = []}
                , Team { teamName = "Padres"
                       , city = "San Diego"
                       , players = []}
                , Team { teamName = "Giants"
                       , city = "San Francisco"
                       , players = []}
                ])
            ])
        ]
      }

-- ------------------------------------------------------------