https://github.com/olk/closures
closures in R, Python and C++
https://github.com/olk/closures
closures cpp python r
Last synced: about 1 month ago
JSON representation
closures in R, Python and C++
- Host: GitHub
- URL: https://github.com/olk/closures
- Owner: olk
- License: gpl-3.0
- Created: 2018-12-21T07:40:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-03-01T07:17:49.000Z (over 5 years ago)
- Last Synced: 2025-03-26T18:17:27.913Z (about 1 year ago)
- Topics: closures, cpp, python, r
- Language: C++
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# closures
A closure is a named or anonymous function that captures non-global variables
that are defined outside of the function body.
C++:
```cpp
auto make_fn(int x = 0) {
return [x]() mutable {
x += 1;
return x;
};
}
```
Python:
```python
def make_fn(x=0):
def closure():
nonlocal x
x += 1
return x
return closure
```
R:
```r
make_fn <- function(x=0) {
closure <- function() {
x <<- x+1
x
}
return(closure)
}
```