Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jimhester/lambda

Simple anonymous functions
https://github.com/jimhester/lambda

Last synced: 3 months ago
JSON representation

Simple anonymous functions

Awesome Lists containing this project

README

        

---
output:
md_document:
variant: markdown_github
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# Lambda

[![Travis-CI Build Status](https://travis-ci.org/jimhester/lambda.svg?branch=master)](https://travis-ci.org/jimhester/lambda)
[![Coverage Status](https://img.shields.io/codecov/c/github/jimhester/lambda/master.svg)](https://codecov.io/github/jimhester/lambda?branch=master)

Lambda contains only one function `f()`, which allows you to write new
functions in a very compact format.

Simply write the body of the function as you would normally. If you want a
given variable to appear as one of the arguments to the function, surround it with
`.()`. Default arguments can be also be specified within `.()` as you normally
would in the argument list.

```{r}
library(lambda)

function(x, y) x + y
f(.(x) + .(y))

Reduce(f(.(x) + .(y)), 1:10)

add <- f(.(x) + .(y = 5))
add

add(1)

add(1, 2)
```

It works very well as a way to compactly define simple utility functions
```{r}
x <- list(1, list(NULL), 2)
compact <- f(Filter(Negate(is.null), .(x)))
compact
compact(x)
```

Or for partial function application
```{r}
f1 <- f(runif(n = rpois(1, 5), .(...)))
f1
```

## References

For a different (better?) approach to this same idea, see
[pryr::f()](https://github.com/hadley/pryr/blob/master/R/f.r).