Skip to content

D-Se/ask

Repository files navigation

ask ? yes ~ no

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.

Usage

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

But wha-

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

Other packages

  • Control flow: data.table, kit, dplyr, rtern, collapse
  • Coercion: vctrs, rlang
  • Errors: rlang, tryCatchLog

About

R "ask" operator ?~

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published