Skip to content

Commit 466705b

Browse files
committed
Prepare tutorials for 0.2.0.0
1 parent 01fbc8f commit 466705b

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

docs/coming_from_dplyr.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ starwars %>%
105105
Our logic is more explicit about what's going on. Because both our fields are nullable/optional we have to specify the type.
106106

107107
```haskell
108-
bmi (w :: Int) (h :: Int) = (fromIntegral w) / (fromIntegral h / 100) ** 2 :: Double
108+
convertEitherToDouble name d = D.apply (either (\unparsed -> if unparsed == "NA" then Nothing else D.readDouble unparsed) (Just . (fromIntegral @Int))) name d
109109

110110
starwars
111+
|> D.fold convertEitherToDouble ["mass", "height"]
111112
|> D.selectRange ("name", "mass")
112-
-- mass and height are optionals so we combine them with
113-
-- Haskell's Applicative operators.
114-
|> D.derive "bmi" (lift2 (/) (lift fromIntegral (col @Int "mass")) (lift fromIntegral (col@ Int "height")))
113+
-- Remove Nothing/empty rows.
114+
|> D.filterJust "mass"
115+
|> D.filterJust "height"
116+
|> D.derive "bmi" ((D.col @Double "mass") / (D.lift2 (**) (D.col @Double "height") (D.lit 2)))
115117
|> D.take 10
116118
```
117119

@@ -221,8 +223,7 @@ starwars |> D.select ["species", "mass"]
221223
-- Always better to be explcit about types for
222224
-- numbers but you can also turn on defaults
223225
-- to save keystrokes.
224-
|> D.filterWhere (["Count", "Mean_mass"],
225-
D.func (\(n :: Int) (mass :: Double) -> n > 1 && mass > 50))
226+
|> D.filterWhere (D.lift2 (&&) (D.lift (>1) (D.col @Int "Count")) (D.lift (>50) (D.col @Int "Mean_mass")))
226227
```
227228

228229
```

docs/coming_from_polars.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ main = do
122122
...
123123
let year = (\(YearMonthDay y _ _) -> y)
124124
print $ df_csv
125-
|> D.derive "birth_year" year "birthdate"
126-
|> D.deriveFrom (["weight", "height"], D.func (\(w :: Double) (h :: Double) -> w / h ** 2))
127-
"bmi"
125+
|> D.derive "birth_year" (lift year (D.col @Date "birthdate"))
126+
|> D.derive "bmi" ((D.col @Double "weight") / (D.lift2 (**) (D.col @Double "height") (D.lit 2)))
128127
|> D.select ["name", "birth_year", "bmi"]
129128
```
130129

@@ -146,8 +145,8 @@ main = do
146145
let bmi :: Double -> Double -> Double
147146
bmi w h = w / h ** 2
148147
print $ df_csv
149-
|> D.derive "birth_year" year "birthdate"
150-
|> D.deriveFrom (["weight", "height"], D.func bmi) "bmi"
148+
|> D.derive "birth_year" (lift year (D.col @Date "birthdate"))
149+
|> D.derive "bmi" ((D.col @Double "weight") / (D.lift2 (**) (D.col @Double "height") (D.lit 2)))
151150
|> D.select ["name", "birth_year", "bmi"]
152151
```
153152

@@ -186,9 +185,8 @@ We instead write this two `applyWithAlias` calls:
186185

187186
```haskell
188187
df_csv
189-
|> D.derive "weight-5%" (*0.95) "weight"
190-
-- Alternatively we can use the `as` function.
191-
|> D.as "height-5%" D.apply (*0.95) "height"
188+
|> D.derive "weight-5%" ((col @Double "weight") * (lit 0.95))
189+
|> D.derive "height-5%" ((col @Double "height") * (lit 0.95))
192190
|> D.select ["name", "weight-5%", "height-5%"]
193191
```
194192

@@ -207,7 +205,7 @@ index | name | height-5% | weight-5%
207205
However we can make our program shorter by using regular Haskell and folding over the dataframe.
208206

209207
```haskell
210-
let reduce name = D.derive (name <> "-5%") (*0.95) name
208+
let reduce name = D.derive (name <> "-5%") ((col @Double name) * (lit 0.95))
211209
df_csv
212210
|> D.fold reduce ["weight", "height"]
213211
|> D.select ["name", "weight-5%", "height-5%"]
@@ -324,7 +322,7 @@ print(result)
324322
```haskell
325323
decade = (*10) . flip div 10 . year
326324
df_csv
327-
|> D.derive "decade" decade "birthdate"
325+
|> D.derive "decade" (lift decade (col @date "birthdate"))
328326
|> D.groupByAgg D.Count ["decade"]
329327
|> D.aggregate [("height", D.Maximum), ("weight", D.Mean)]
330328
|> D.select ["decade", "sampleSize", "Mean_weight", "Maximum_height"]

0 commit comments

Comments
 (0)