All Projects → r-lib → Zeallot

r-lib / Zeallot

Licence: other
Variable assignment with zeal! (or multiple, unpacking, and destructuring assignment in R)

Programming Languages

r
7636 projects

Projects that are alternatives of or similar to Zeallot

Nanomatch
Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but without support for extended globs (extglobs), posix brackets or braces, and with complete Bash 4.3 wildcard support: ("*", "**", and "?").
Stars: ✭ 79 (-61.27%)
Mutual labels:  pattern-matching
Rosie Pattern Language
Rosie Pattern Language (RPL) and the Rosie Pattern Engine have MOVED!
Stars: ✭ 146 (-28.43%)
Mutual labels:  pattern-matching
Symja android library
☕️ Symja - computer algebra language & symbolic math library. A collection of popular algorithms implemented in pure Java.
Stars: ✭ 170 (-16.67%)
Mutual labels:  pattern-matching
Pattern Matching Ts
⚡ Pattern Matching in Typescript
Stars: ✭ 107 (-47.55%)
Mutual labels:  pattern-matching
Z
Pattern Matching for Javascript
Stars: ✭ 1,693 (+729.9%)
Mutual labels:  pattern-matching
Eval
Eval is a lightweight interpreter framework written in Swift, evaluating expressions at runtime
Stars: ✭ 157 (-23.04%)
Mutual labels:  pattern-matching
Glob
Glob for C++17
Stars: ✭ 74 (-63.73%)
Mutual labels:  pattern-matching
Gradoop
Distributed Graph Analytics with Apache Flink
Stars: ✭ 197 (-3.43%)
Mutual labels:  pattern-matching
Stumpy
STUMPY is a powerful and scalable Python library for modern time series analysis
Stars: ✭ 2,019 (+889.71%)
Mutual labels:  pattern-matching
Fpgo
Monad, Functional Programming features for Golang
Stars: ✭ 165 (-19.12%)
Mutual labels:  pattern-matching
Matchpy
A library for pattern matching on symbolic expressions in Python.
Stars: ✭ 109 (-46.57%)
Mutual labels:  pattern-matching
Grape
🍇 Syntax-aware grep-like for Clojure
Stars: ✭ 132 (-35.29%)
Mutual labels:  pattern-matching
Expat
Reusable, composable patterns across Elixir libraries
Stars: ✭ 157 (-23.04%)
Mutual labels:  pattern-matching
Qutuf
Qutuf (قُطُوْف): An Arabic Morphological analyzer and Part-Of-Speech tagger as an Expert System.
Stars: ✭ 84 (-58.82%)
Mutual labels:  pattern-matching
Akar
First-class patterns for Clojure. Made with love, functions, and just the right amount of syntax.
Stars: ✭ 176 (-13.73%)
Mutual labels:  pattern-matching
Patme
Elixir-style pattern matching for ruby methods
Stars: ✭ 75 (-63.24%)
Mutual labels:  pattern-matching
Motif
Scala-like pattern matching for Java 8
Stars: ✭ 149 (-26.96%)
Mutual labels:  pattern-matching
Hexraystoolbox
Hexrays Toolbox - Find code patterns within the Hexrays AST
Stars: ✭ 202 (-0.98%)
Mutual labels:  pattern-matching
Symbolicutils.jl
Expression rewriting and simplification
Stars: ✭ 189 (-7.35%)
Mutual labels:  pattern-matching
Egison Ruby
A Ruby gem for non-linear pattern-matching with backtracking
Stars: ✭ 159 (-22.06%)
Mutual labels:  pattern-matching

zeallot

Variable assignment with zeal!

alt text alt text

What's there to be excited about?

zeallot allows multiple, unpacking, or destructuring assignment in R by providing the %<-% operator. With zeallot you can tighten code with explicit variable names, unpack pieces of a lengthy list or the entirety of a small list, destructure and assign object elements, or do it all at once.

Unpack a vector of values.

c(x, y) %<-% c(0, 1)
#> x
#[1] 0
#> y
#[1] 1

Unpack a list of values.

c(r, d) %<-% list(2, 2)
#> r
#[1] 2
#> d
#[1] 2

Destructure a data frame and assign its columns.

c(duration, wait) %<-% head(faithful)

#> duration
#[1] 3.600 1.800 3.333 2.283 4.533 2.883
#> wait
#[1] 79 54 74 62 85 55

Unpack a nested list into nested left-hand side variables.

c(c(a, b), c(c, d)) %<-% list(list(1, 2), list(3, 4))
#> a
#[1] 1
#> b
#[1] 2
#> c
#[1] 3
#> d
#[1] 4

Destructure and partially unpack a list. "a" is assigned to first, but "b", "c", "d", and "e" are grouped and assigned to one variable.

c(first, ...rest) %<-% list("a", "b", "c", "d", "e")
first
#[1] "a"
rest
#[[1]]
#[1] "b"
#
#[[2]]
#[1] "c"
#
#[[3]]
#[1] "d"
#
#[[4]]
#[1] "e"

Installation

You can install zeallot from CRAN.

install.packages("zeallot")

Use devtools to install the latest, development version of zeallot from GitHub.

devtools::install_github("nteetor/zeallot")

Getting Started

Below is a simple example using the purrr package and the safely function.

Safe Functions

The purrr::safely function returns a "safe" version of a function. The following example is borrowed from the safely documentation. In this example a safe version of the log function is created.

safe_log <- purrr::safely(log)
safe_log(10)
#$result
#[1] 2.302585
#
#$error
#NULL

safe_log("a")
#$result
#NULL
#
#$error
#<simpleError in .f(...): non-numeric argument to mathematical function>

A safe function always returns a list of two elements and will not throw an error. Instead of throwing an error, the error element of the return list is set and the value element is NULL. When called successfully the result element is set and the error element is NULL.

Safe functions are a great way to write self-documenting code. However, dealing with a return value that is always a list could prove tedious and may undo efforts to write concise, readable code. Enter zeallot.

The %<-% Operator

With zeallot's unpacking operator %<-% we can unpack a safe function's return value into two explicit variables and avoid dealing with the list return value all together.

c(res, err) %<-% safe_log(10)
res
#[1] 2.302585
err
#NULL

The name structure of the operator is a flat or nested set of bare variable names built with calls to c(). . These variables do not need to be previously defined. On the right-hand side is a vector, list, or other R object to unpack. %<-% unpacks the right-hand side, checks the number of variable names against the number of unpacked values, and then assigns each unpacked value to a variable. The result, instead of dealing with a list of values there are two distinct variables, res and err.

Further Reading and Examples

For more on the above example, other examples, and a thorough introduction to zeallot check out the vignette on unpacking assignment.

Below are links to discussions about multiple, unpacking, and destructuring assignment in R,

Related work

The vadr package includes a bind operation with much of the same functionality as %<-%. As the author states, "[they] strongly prefer there to be a <- anywhere that there is a modification to the environment." If you feel similarly I suggest looking at vadr. Unfortunately the vadr package is not on CRAN and will need to be installed using devtools::install_github().


Thank you to Paul Teetor for inspiring me to build zeallot.

Without his encouragement nothing would have gotten off the ground.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].