Skip to content

[WIP] Module on "Writing functions" - #3

Draft
etiennebacher wants to merge 15 commits into
mainfrom
module-writing-functions
Draft

[WIP] Module on "Writing functions"#3
etiennebacher wants to merge 15 commits into
mainfrom
module-writing-functions

Conversation

@etiennebacher

Copy link
Copy Markdown
Collaborator

Very early, incomplete draft.

@github-actions

github-actions Bot commented Jun 30, 2026

Copy link
Copy Markdown

See preview at https://palaeoverse.github.io/modules/pr-preview/pr-3/training/modules

Comment on lines +372 to +373
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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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.

Comment on lines +384 to +390
```{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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this section is useful to know, but this module is about writing functions so I am not entirely convinced it fits here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants