Chapter 5 Code Optimisation

5.1 Compose functions

5.1.1 I want to…

Write a function that wraps other functions.

5.1.2 Here’s how to:

term estimate std.error statistic p.value
(Intercept) 5.006 0.0728022 68.761639 0
Speciesversicolor 0.930 0.1029579 9.032819 0
Speciesvirginica 1.582 0.1029579 15.365506 0

5.1.3 Ok, but why?

compose(y, x) composes a function that will do y(x()).

5.2 Prefilled functions

5.2.1 I want to…

Prefill a function so that I won’t have to specify the arguments any time I use it.

5.2.3 Ok, but why?

partial(f, args = "x") returns a function with a prefilled function with args = "x".

5.3 Negate a function

5.3.1 I want to…

Inverse what a function does.

5.3.3 Ok, but why?

negate(f) returns a function that does !f(...).

5.3.4 See also


5.4 Negate a function

5.4.1 I want to…

Change what input my function can take.

5.4.2 Here’s how to:

## [1] FALSE FALSE FALSE  TRUE

5.4.3 Ok, but why?

The lift_* family of functions change the type of input of other function.

5.4.4 See also