1 Control Flow fa-edit

2 File Manipulation fa-edit

Treadway (2017), Treadway (2018a), and Treadway (2018b)

3 Function Factories fa-edit

A function factory is a function that makes functions (Wickham 2019).

3.1 Random Number Genrator

Create rand(), a function factory that creates different kinds of random number generators when different parameters are set.

R

rand <- function(seed = as.double(Sys.time()), upper = 99999, rng_one = T) {
  force(seed)
  function() {
    rand_num <- (1664525 * seed + 1013904223) %% upper
    seed <<- seed + 1
    if (rng_one) return(rand_num/upper)
    else return(rand_num)
  }
}
rand2019 <- rand(seed = 2019)
rand2019()
#> [1] 0.2394424
rand2019()
#> [1] 0.8848588

Python

import time
def rand(seed=time.time(), upper=99999, rng_one=True):
    def randgen():
        nonlocal seed
        rand_num = (1664525 * seed + 1013904223) % upper
        seed += 1
        if rng_one: return(rand_num/upper)
        else: return(rand_num)
    return(randgen)
rand2019 = rand(seed=2019)
print(rand2019())
#> 0.23944239442394424
print(rand2019())
#> 0.8848588485884858

See Klein (2019) for more about global, local, and nolocal variables.

4 Regular Expressions fa-edit

5 Interacting with Web APIs fa-edit

References

Klein, Bernd. 2019. “Python Tutorial: Global Vs. Local Variables and Namespaces.” 2019. https://www.python-course.eu/python3_global_vs_local_variables.php.

Treadway, Andrew. 2017. “File Manipulation with Python - Open Source Automation.” August 31, 2017. http://theautomatic.net/2017/08/31/file-manipulation-with-python/.

———. 2018a. “R: How to Create, Delete, Move, and More with Files.” Open Source Automation. July 11, 2018. http://theautomatic.net/2018/07/11/manipulate-files-r/.

———. 2018b. “10 R Functions for Linux Commands and Vice-Versa.” Open Source Automation. December 10, 2018. http://theautomatic.net/2018/12/10/10-r-functions-for-linux-commands-and-vice-versa/.

Wickham, Hadley. 2019. “Function Factories.” Advanced R. 2019. https://adv-r.hadley.nz/.