matloff / matloff/partools

More helper functions

Open
#2 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
40
Forks
12
PR merge metrics
No merged PRs in 30d

Description

I think the code in partools could be made substantially more elegant with a few helper functions:

``` R
clusterRequireNamespace <- function(cl, pkg) {
clusterCall(cl, requireNamespace, package = pkg, quietly = TRUE)
}
clusterRequirePackage <- function(cl, pkg) {
clusterCall(cl, library, package = pkg, character.only = TRUE)
}
clusterEval < function(cl, expr) {
clusterCall(cl, eval, expr, env = .GlobalEnv)
}

clusterAssign <- function(cl, name, value) {
clusterCall(cl, assign, x = name, value = value)
}

asCall <- function(x) {
if (inherits(x, "formula")) {
stopifnot(length(x) == 2)
x[[2]]
} else if (is.atomic(x) || is.name(x) || is.call(x)) {
x
} else {
stop("Unknown input")
}
}

clusterEvalI <- function(cl, expr, ...) {
args <- lapply(list(...), asCall)
interpolated <- methods::substituteDirect(asCall(expr), args)

clusterEval(cl, interpolated)
}
```

This allows (e.g.) `fileread` to be rewritten as:

``` R

fileread <- function (cls, fname, dname, ndigs, header = FALSE, sep = " ",
usefread = FALSE) {
if (usefread) {
clusterRequireNamespace(cls, "data.table")
clusterEvalI(cls, ~ myfread <- data.table::fread))
} else {
clusterEvalI(cls, ~ myfread <- read.table))
}

clusterEvalI(cl,
~ `_mychunk` <- filechunkname(fname, ndigs),
fname = fname, ndig
s = ndgs
)

clusterEvalI(cls,
~ dname <- myfread(`_mychunk`, header = header, sep = sep),
dname = as.name(dname),
header = header,
sep = sep
)
}
```

which I think is rather more clear since it focusses on the computation, rather than the details of the call manipulation (and working with the AST is obviously more elegant than working with strings)

(Also note the use of `_mychunk` as a variable name - using a non syntactic variable should decrease the chances of accidentally clobbering an object that the user created)

I haven't tested this code yet, but I'm happy to do so if you're interested in the idea.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue proposes helper functions for cluster calls and AST substitution, including a rewrite of fileread, but names no files or tests. Start by locating the existing fileread and related call-manipulation code, then assess the proposed helpers and test the behavior. Done means the helpers are implemented and the fileread example works without regressions.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
distributed-systems, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.