[WIP] Module on "Writing functions" - #3
Conversation
…their own section
| In R, functions are treated as *first order citizens*, meaning they can | ||
| be assigned to variables and passed over to other functions. Let's look |
There was a problem hiding this comment.
I think I only saw the "first-order citizens" expression in programming discussions but it's really not clear to me. Is it commonly used in English?
Something like this would be clearer IMO:
| In R, functions are treated as *first order citizens*, meaning they can | |
| be assigned to variables and passed over to other functions. Let's look | |
| In R, functions are just another type of objects, meaning that they can be updated | |
| by the user, assigned to variables, and passed over to other functions. |
| ```{r} | ||
| typeof(my_variable) | ||
| typeof(TRUE) # for comparison | ||
| ``` | ||
|
|
||
| This shows that `my_variable` is what's called a *closure*, which is a | ||
| special type of function. |
There was a problem hiding this comment.
IMO this adds complexity for little benefit, I'd rather remove it. If we keep this, then there should be a couple sentences about why a closure is a special type of function and it should give an example of a function that is not a closure.
| Working through the source code of functions can be very insightful, but | ||
| can quickly expose you to a lot of low-level functionality of R that | ||
| comes with a steep learning curve. As an (advanced) exercise, have a | ||
| look at the implementation of `mean` or `lapply` and try to understand |
There was a problem hiding this comment.
mean is an S3 generic so printing its source code (just UseMethod("mean")) is quite confusing (maybe that's what you wanted, I'm not sure). sd isn't generic so it's more straightforward:
| look at the implementation of `mean` or `lapply` and try to understand | |
| look at the implementation of `sd` or `lapply` and try to understand |
|
|
||
| # Other ways to use and create functions | ||
|
|
||
| ## Functions as arguments |
There was a problem hiding this comment.
I think this section is useful to know, but this module is about writing functions so I am not entirely convinced it fits here.
Very early, incomplete draft.