knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
Functional programming in R.
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("armcn/fp")
library(fp, warn.conflicts = FALSE) numbers <- 1:10 numbers_list <- as.list(numbers) map(\(a) a + 1, numbers) map2(`+`, numbers, numbers) filter(\(a) a > 5, numbers) reduce(`+`, 0, numbers) scan(`*`, 1, numbers) any(\(a) a %% 2 == 0, numbers) all(\(a) a %% 2 == 0, numbers) member(5, numbers) first(numbers_list) last(numbers_list) head(numbers_list) tail(numbers) init(numbers) get_at(3, numbers_list)
square <- \(a) a ^ 2 is_even <- \(a) a %% 2 == 0 numbers |> map(square)() |> filter(is_even)() |> reduce(`+`, 0)() numbers %|>% map(square) %|>% filter(is_even) %|>% reduce(`+`, 0) pipe( map(square), filter(is_even), reduce(`+`, 0) )(numbers) compose( reduce(`+`, 0), filter(is_even), map(square) )(numbers) sum_even_squares <- reduce(`+`, 0) %.% filter(is_even) %.% map(square) sum_even_squares(numbers)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.