{"id":16572277,"url":"https://github.com/jonocarroll/weasel","last_synced_at":"2026-06-10T14:30:57.490Z","repository":{"id":174089023,"uuid":"651394767","full_name":"jonocarroll/weasel","owner":"jonocarroll","description":"Provides `pop()` and `push()` functionality","archived":false,"fork":false,"pushed_at":"2023-06-10T01:52:40.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-09T12:55:07.741Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonocarroll.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["jonocarroll"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2023-06-09T06:32:33.000Z","updated_at":"2024-02-26T17:36:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"bf169aae-65d7-441f-a787-ed3b8501d288","html_url":"https://github.com/jonocarroll/weasel","commit_stats":null,"previous_names":["jonocarroll/weasel"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jonocarroll/weasel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonocarroll%2Fweasel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonocarroll%2Fweasel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonocarroll%2Fweasel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonocarroll%2Fweasel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonocarroll","download_url":"https://codeload.github.com/jonocarroll/weasel/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonocarroll%2Fweasel/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34157453,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-10T02:00:07.152Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T21:27:00.330Z","updated_at":"2026-06-10T14:30:57.472Z","avatar_url":"https://github.com/jonocarroll.png","language":"R","funding_links":["https://github.com/sponsors/jonocarroll"],"categories":[],"sub_categories":[],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\nlibrary(weasel)\n```\n\n# weasel \u003cimg src=\"man/figures/logo.png\" align=\"right\" height=\"139\" /\u003e\n\n\u003c!-- badges: start --\u003e\n\u003c!-- badges: end --\u003e\n\n\u003e *'Round and 'round the R workspace, code-monkey chased the {weasel}*\n\u003e\n\u003e *Code-monkey stopped to extract the first val...*\n\u003e\n\u003e *`pop()` goes the {weasel}*\n\n## Installation\n\nYou can install the development version of weasel like so:\n\n``` r\n# install.packages(\"remotes\")\nremotes::install_github(\"jonocarroll/weasel\")\n```\n\n## Motivation\n\nInspired by other (less functional/immutable) languages where one might be \nfamiliar with being able to `pop()` a value from the top of the stack, this \npackage provides similar functionality.\n\nIf we define a vector `a` containing some values\n\n```{r}\na \u003c- c(3, 1, 4, 1, 5, 9)\n```\n\nand we wish to extract the first value, we can certainly do so with \n\n```{r}\na[1]\n```\n\nbut, due to the nature of R, the vector `a` is unchanged\n\n```{r}\na\n```\n\nInstead, we could remove the first value of `a` with\n\n```{r}\na[-1]\n```\n\nbut again, `a` remains unchanged - in order to modify `a` we must redefine it as e.g.\n\n```{r}\na \u003c- a[-1]\na\n```\n\nThis differs from other languages were we might want to _extract_ that first value for use, \nand leave the rest of the vector in place.\n\n## Usage\n\n{weasel} offers that functionality\n\n```{r}\na \u003c- c(3, 1, 4, 1, 5, 9)\na\nfirst_val \u003c- pop(a)\na\nfirst_val\n```\n\nNote that this did not require explicitly^[It certainly is redefined internally, but \nthe use of `defmacro()` means it does not need to be explicit] redefining `a`; \nthe return value of `pop()` is the popped value.\n\nThis is also a Generic, so it is defined for some classes (`vector`, `list`, `data.frame`) \nand can be extended.\n\nWith a `list`:\n\n```{r}\na \u003c- list(x = c(2, 3), y = c(\"foo\", \"bar\"), z = c(3.1, 4.2, 6.9))\na\nx \u003c- pop(a)\na\nx\n```\n\nWith a `data.frame`:\n\n```{r}\na \u003c- data.frame(x = c(2, 3, 4), y = c(\"foo\", \"bar\", \"baz\"), z = c(3.1, 4.2, 6.9))\na\nx \u003c- pop(a)\na\nx\n```\n\n{weasel} also offers `push()` functionality\n\n```{r}\na \u003c- c(1, 4, 1, 5, 9)\na\npush(a, 3)\na\n```\n\nwhich is also Generic\n\n```{r}\na \u003c- list(y = c(\"foo\", \"bar\"), z = c(3.1, 4.2, 6.9))\na\npush(a, list(new_vals = c(99, 77)))\na\n\na \u003c- data.frame(y = c(\"foo\", \"bar\", \"baz\"), z = c(3.1, 4.2, 6.9))\na\npush(a, data.frame(y = 99, z = 77))\na\n```\n\n## Warnings\n\nPlease note that this is intended only for informal use and education. This \nfunctionality is **not** idiomatic R and is more of a footgun than should be \ntolerated.\n\n## Explanation\n\nInternally, the evaluation of each 'method' is a macro defined using `gtools::defmacro()` \nwhich enables substitution of input arguments and redefinition in the parent namespace.\n\n[This blogpost](https://jcarroll.com.au/2023/06/10/reflecting-on-macros/) covers the full details. [This article](https://www.r-project.org/doc/Rnews/Rnews_2001-3.pdf) in the R News Newsletter \ncovers the construction and motivation of `defmacro()`.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonocarroll%2Fweasel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonocarroll%2Fweasel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonocarroll%2Fweasel/lists"}