Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ S3method(summary,rlang_trace)
S3method(summary,rlang_warning)
export("!!!")
export("!!")
export("%&&%")
export("%<~%")
export("%@%")
export("%@%<-")
Expand Down
16 changes: 16 additions & 0 deletions R/operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ if (exists("%||%", envir = baseenv())) {
`%||%` <- get("%||%", envir = baseenv())
}

#' Default value for non-`NULL`
#'
#' This infix operator is the conceptual opposite of %||%, providing a fallback
#' only if x is defined.
#'
#' @param x,y If `x` is NULL, will return `x`; otherwise returns `y`.
#' @export
#' @name op-null-continuation
#' @seealso [op-null-default]
#' @examples
#' 1 %&&% 2
#' NULL %&&% 2
`%&&%` <- function(x, y) {
if (!is_null(x)) y else x
}

`%|0|%` <- function(x, y) {
if (!length(x)) y else x
}
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,6 @@ reference:
- subtitle: Operators
contents:
- "`%||%`"
- "`%&&%`"
- "`%|%`"
- "`%@%`"
23 changes: 23 additions & 0 deletions man/op-null-continuation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ test_that("%@% works with S4 objects (#207)", {
expect_identical(fievel@name, "Bernard")
expect_identical(fievel@species, "MOUSE")
})

test_that("%&&% works", {
expect_null(NULL %&&% 2)
expect_null(1 %&&% NULL)
expect_equal(1 %&&% 2, 2)
})
Loading