GithubHelp home page GithubHelp logo

lambda's Introduction

Lambda

Travis-CI Build Status Coverage Status

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.

library(lambda)

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

Reduce(f(.(x) + .(y)), 1:10)
#> [1] 55

add <- f(.(x) + .(y = 5))
add
#> function (x, y = 5) 
#> x + y

add(1)
#> [1] 6

add(1, 2)
#> [1] 3

It works very well as a way to compactly define simple utility functions

x <- list(1, list(NULL), 2)
compact <- f(Filter(Negate(is.null), .(x)))
compact
#> function (x) 
#> Filter(Negate(is.null), x)
compact(x)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [[2]][[1]]
#> NULL
#> 
#> 
#> [[3]]
#> [1] 2

Or for partial function application

f1 <- f(runif(n = rpois(1, 5), .(...)))
f1
#> function (...) 
#> runif(n = rpois(1, 5), ...)

References

For a different (better?) approach to this same idea, see pryr::f().

lambda's People

Contributors

jimhester avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.