Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/egnha/dub
Unpacking assignment via pattern matching
https://github.com/egnha/dub
pattern-matching r unpacking-assignment
Last synced: 3 months ago
JSON representation
Unpacking assignment via pattern matching
- Host: GitHub
- URL: https://github.com/egnha/dub
- Owner: egnha
- License: other
- Created: 2018-02-08T07:13:13.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-28T14:07:22.000Z (about 6 years ago)
- Last Synced: 2024-10-01T16:36:57.643Z (4 months ago)
- Topics: pattern-matching, r, unpacking-assignment
- Language: R
- Homepage:
- Size: 99.6 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```[![Travis build status](https://travis-ci.org/egnha/dub.svg?branch=master)](https://travis-ci.org/egnha/dub)
[![Coverage status](https://codecov.io/gh/egnha/dub/branch/master/graph/badge.svg)](https://codecov.io/github/egnha/dub?branch=master)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/dub)](https://cran.r-project.org/package=dub)# dub
_dub_ is a small, single-purpose R package for _unpacking assignment_: it provides an operator `%<<-%` that enables you to assign (nested) components of a list (or vector) to names via pattern matching. The pattern matching syntax mirrors the semantics of `list()`. Think of the “dub(ble) arrow” `<<-` as a pictograph representing multiple `<-`'s.
```{r}
library(dub)(one : two : three) %<<-% 1:3
one
two
three(x) %<<-% list(list("x"))
((y)) %<<-% list(list("y"))
x
y(u : (v : w)) %<<-% list(1, list(2, 3:4))
u
v
w# Use . to drop specific components, ... to drop greedily (the _ in Haskell)
(. : width : ... : species) %<<-% iris
head(width)
head(species)
```More details and examples are in the package documentation (`` ?`%<<-%` ``).
## Installation
Install from CRAN:
``````{r, eval = FALSE}
install.packages("dub")
```Alternatively, install the development version from GitHub:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("egnha/dub")
```## Prior art
Unpacking/multiple assignment appears in other languages (e.g., [Python](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences), [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment), [Clojure](https://clojure.org/guides/destructuring)). While R has no such feature, using a custom operator to do this has long been a folklore method. To my knowledge, the earliest implementation is by [Gabor Grothendieck](https://stat.ethz.ch/pipermail/r-help/2004-June/053343.html) (2004), cf. `list` in the [gsubfn](https://cran.r-project.org/package=gsubfn) package.
If you need specialized forms of "destructured" assignment, I recommend the
[zeallot](https://github.com/nteetor/zeallot) package by [Nate
Teetor](https://github.com/nteetor).## License
MIT Copyright © 2018 [Eugene Ha](https://github.com/egnha)