Skip to content

Conversation

@hughjonesd
Copy link

This is a build of R that adds an experimental pipe assignment operator <|>.

LHS <|> RHS is parsed as LHS <- RHS(LHS, ...).

Examples

x <|> sqrt()             # becomes  x <- sqrt(x)
x[item] <|> foo()       # becomes x[item] <- foo(x[item])
x$elem <|> foo()        # becomes  x$elem <- foo(x$elem)
names(x) <|> toupper()  # becomes  names(x) <- toupper(names(x))
x <|> gsub("pattern", "replacement", x = _)
                        # becomes  x <- gsub("pattern", "replacement", x = x)

Arbitrarily complex left hand sides are allowed, including assignment functions and subassignment.

Chaining <|> is not allowed (and would not make sense). The placeholder works, but placeholder use in e.g. _$foo$bar is not.

Rationale

For a detailed rationale, see https://hughjonesd.github.io/case-for-pipe-assignment.html.

The skinny:

  • R is pass-by-value and allows complex subassignment
  • Often, one wants to simply pass variables, or parts of them, through a function
  • This involves repeating oneself
  • Pipe assignment can make code clearer by avoiding the repetition

In other words, these:

names(x)[1:5] <|> toupper()
my_data[rows, cols] <|> as.numeric()

may be easier to read (and to write!) than these:

names(x)[1:5] <- toupper(names(x)[1:5])
my_data[rows, cols] <- as.numeric(my_data[rows, cols])

Vibe-coded

Yeah, this was vibe-coded! Claude basically one-shotted the Yacc, which is much beyond my capability.

hughjonesd and others added 2 commits December 7, 2025 08:05
LHS <|> RHS is parsed as LHS <- RHS(LHS, ...).

Example:
  x <|> sqrt()             # becomes  x <- sqrt(x)
  names(x) <|> toupper()   # becomes  names(x) <- toupper(names(x))
  x <|> gsub("pattern", "replacement", x = _)
                           # becomes  x <- gsub("pattern", "replacement", x = x)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant