Ternary R operator that unifies if
, ifelse
, is
,
as
, stopifnot
and documentation search. Elegant, terse, yet fast, optimize for thought-to-code and code-to-thought.
remotes::install_github("D-Se/ask")
# ternary vectorized question with elevated precedence
x <- c(TRUE, FALSE, NA)
z <- x ? 1:3 ~ 7:9 # c(1, 8, NA)
x ? 1:3 ~ (!x ? 4:7 ~ 8:10) # c(1, 5, NA)
# check types by abbreviation or template
5 ? num # TRUE
5 ? 1 # TRUE
# coerce types
5 ?~ chr # "5"
5 ?~ "" # "5"
# negate by passing !
5 ?! num # FALSE
# scalar-if semantics
TRUE ? 1 ~ 2 # 1
TRUE ? 1 # 1
FALSE ? 2 # NULL (base R silent default)
FALSE ?~ 2 # 2
# happy path guard clauses
TRUE ?~! "message" # NULL
FALSE ?~! "message" # error: message
# chain questions from left to right
y <- (5 ?! NULL ? "yay") # y <- if(!is.null(5)) "yay"
# search documentation like usual*
?integer
??regression
View help via ?`?`
.
Atomic Bunch Lang Other
--------------------------------
atm rec lng na
lgl lst sym nan
int dfr exp nil
num vec call fin
chr mtx name inf
raw arr fun ord
cmp fct env prim
dbl fml tab
ask
:
- is built on, and approximately as fast as,
data.table
(benchmark). - has no dependencies.
- sells
methods?topic
, and buys at a performance cost- S4 method documentation is dropped.
?
incurs overhead from rectifying operator precedence and R function calls.
- balances readability, correctness and conventions
# all consequents must be of the same type
x ? 1:3 ~ c(5, 6, 7) # integer ≠ numeric
# for nested ifelse, ( or { is required
x ? a ~ !x ? b ~ c # precedence issue
- Control flow:
data.table
,kit
,dplyr
,rtern
,collapse
- Coercion:
vctrs
,rlang
- Errors:
rlang
,tryCatchLog