Skip to content

Commit ea98f58

Browse files
committed
Change chipotle example to showcase multiple aggregation constructs that leverage actual haskell.
1 parent 36d1081 commit ea98f58

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

app/Main.hs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
44
{-# LANGUAGE TypeApplications #-}
5+
{-# LANGUAGE TupleSections #-}
56

67
module Main where
78

@@ -92,11 +93,9 @@ chipotle = do
9293
-- Change a specfic order ID
9394
|> D.applyWhere (== 1) "order_id" (+ 2) "quantity"
9495
-- Index based change.
95-
|> D.applyAtIndex 0 (flip (-) 2) "quantity"
96-
-- drop dollar sign and parse price as double
97-
|> D.apply (D.readValue @Double . T.drop 1)"item_price"
98-
-- Custom parsing
99-
|> D.apply toIngredientList "choice_description"
96+
|> D.applyAtIndex 0 (\n -> n - 2) "quantity"
97+
-- Custom parsing: drop dollar sign and parse price as double
98+
|> D.apply (D.readValue @Double . T.drop 1) "item_price"
10099

101100
-- sample the dataframe.
102101
print $ D.take 10 f
@@ -124,15 +123,21 @@ chipotle = do
124123
-- It's more efficient to filter before grouping.
125124
|> D.filter "item_name" (searchTerm ==)
126125
|> D.groupBy ["item_name"]
127-
|> D.aggregate (zip (repeat "quantity") [D.Maximum, D.Mean, D.Sum])
126+
-- can also be written as:
127+
-- D.aggregate (zip (repeat "quantity") [D.Sum, D.Maximum, D.Mean])
128+
|> D.aggregate (map ("quantity",) [D.Sum, D.Maximum, D.Mean])
129+
-- Automatically create a variable called <Agg>_<variable>
128130
|> D.sortBy D.Descending ["Sum_quantity"]
129131

130132
-- Similarly, we can aggregate quantities by all rows.
131133
print $
132134
f
133135
|> D.select ["item_name", "quantity"]
134136
|> D.groupBy ["item_name"]
135-
|> D.aggregate (zip (repeat "quantity") [D.Maximum, D.Mean, D.Sum])
137+
-- Aggregate written more explicitly.
138+
-- We have the full expressiveness of Haskell and we needn't fall
139+
-- use a DSL.
140+
|> D.aggregate [("quantity", D.Maximum), ("quantity", D.Mean), ("quantity", D.Sum)]
136141
|> D.take 10
137142

138143
let firstOrder =
@@ -141,13 +146,3 @@ chipotle = do
141146
|> D.filterBy (("Chicken Bowl" :: T.Text) ==) "item_name"
142147

143148
print $ D.take 10 firstOrder
144-
145-
-- An example of a parsing function.
146-
toIngredientList :: Maybe T.Text -> Maybe [T.Text]
147-
toIngredientList Nothing = Nothing
148-
toIngredientList (Just v)
149-
| v == "" = Just []
150-
| v == "NULL" = Nothing
151-
| T.isPrefixOf "[" v = toIngredientList $ Just $ T.init (T.tail v)
152-
| not (T.isInfixOf "," v) = Just [v]
153-
| otherwise = foldl (\a b -> (++) <$> a <*> b) (Just []) (map (toIngredientList . Just . T.strip) (D.splitIgnoring ',' '[' v))

0 commit comments

Comments
 (0)