nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
338 stars 15 forks source link

incorporate `tibble::lst` ? #93

Closed bbolker closed 2 years ago

bbolker commented 3 years ago

"Self-naming lists" are an amazingly handy and surprisingly missing component of base R. This is available in Hmisc::llist() and in a non-exported function in lme4 (lme4:::namedList) (see also this StackOverflow question). tibble::lst() also does sequential construction (i.e. later values on the list can depend on values of elements earlier in the list), which seems handy and worth implementing for compatibility with tibble::lst ...

If you want a PR let me know and I will see what I can do (the naming parts are easy, I haven't tried the sequential construction stuff but seems straightforward enough?)

nathaneastwood commented 3 years ago

Hi @bbolker, this seems like a pretty smart function to include. If you'd like to raise a PR then I'd be happy to take a look or I can maybe try and get around to adding something this week. FWIW, the dots can be handled by poorman:::dotdotdot().

nathaneastwood commented 2 years ago

This topic came up today in work and I managed to have an initial stab at it.

lst <- function(...) {
  fnCall <- match.call()
  listToEval <- as.list(fnCall)[-1]

  out <- vector(mode = "list", length = length(listToEval))
  names(out) <- names(listToEval)
  for (element in seq_along(listToEval)) {
    value <- listToEval[[element]]
    if (is.language(value)) {
      value <- eval(value, envir = listToEval)
    }
    out[[element]] <- value
  }
  out
}

x <- lst(a = 4, b = 5 + c, c = 2)
print(x)

I need to test it - probably using the tibble::lst() tests really - to try and catch any edge cases I am not initially thinking of.