Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 7 and Chapter 8 MH notes #8

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ book-club
A collection of notes from going through different statistical and programming texts.

First on the docket is [**Advanced R**](http://adv-r.had.co.nz/) by Hadley Wickham.
"test"
Test line
Another test line
adding one more
adding a line again
28 changes: 28 additions & 0 deletions advanced_r/ch10_functional_programming_mh.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: "Chapter 10 Notes"
author: "M Hannum"
date: "`r Sys.Date()`"
output:
github_document:
toc: true
toc_depth: 2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(pryr)
```

# [Chapter 10: Functional Programming](http://adv-r.had.co.nz/Functional-programming.html)

1. Functional = Take function as an argument

2. "Anonymous function" which I knew after experience but never thought of as a concept

Exercises
```{r}
cv_list <- lapply(mtcars, function(x) sd(x)/mean(x)*100)
```


80 changes: 80 additions & 0 deletions advanced_r/ch11_functional_programming_neweditionch9_mh.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: "Section IV Metaprogramming"
author: "M Hannum"
date: "`r Sys.Date()`"
output:
github_document:
toc: true
toc_depth: 2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(rlang)
library(lobstr)
```

# [Section IV Metaprogramming Chapter 17: Big Picture](https://adv-r.hadley.nz/metaprogramming.html)

*Metaprogramming* ... "code is data" so you can do cool things with it. "hardest topic in this book"

*Non-standard evaluation* ...

## Code is data

```{r}
expr(10 + 100)

```


vs `enexpr()`


"Captured expressions behave like lists" -
I've been using this without really knowing what I'm doing! Trial and error using `[[` and `$`.

## Code is a tree

*Abstract syntax tree* i.e. AST

```{r}
lobstr::ast(f1(f2(a, b), f3(1, f4(2))))
#> █─f1
#> ├─█─f2
#> │ ├─a
#> │ └─b
#> └─█─f3
#> ├─1
#> └─█─f4
#> └─2
```

## Code can generate code

Using `rlang::call2() to create new trees from components. Function, then arguments.

(** Similar to `do.call`)? Except it doesn't evaluate?

```{r}
?do.call

?call2
```

```{r}
call2("f", 1, 2, 3)
#> f(1, 2, 3)
call2("+", 1, call2("*", 2, 3))
#> 1 + 2 * 3

call2("+", 1, 2)
do.call("+", args = list(1, 2))

```





Loading