Michele
Michele

Reputation: 8753

%between% function possible improvement

As per data.table ver 1.8.8 %between% is defined as follow:

> `%between%`
function (x, y) 
between(x, y[1], y[2], incbounds = TRUE)
<bytecode: 0x0000000050912810>
<environment: namespace:data.table>

I thought that, with this tiny change, this function would be vectorised,

function (x, y) 
    between(x, y[[1]], y[[2]], incbounds = TRUE)

like between

s <- c(2, 5)
d <- c(7, 9)
> between(3, s, d)
[1]  TRUE FALSE

The idea came from having a list with two vectors, which suggested me this possible usage:

`between2%` <- function(x, lst) between(x, lst[[1]], lst[[2]], incbounds = TRUE)

> 3%between%c(s,d)
[1] TRUE
> 3%between2%list(s,d)
[1]  TRUE FALSE

My question is: if I replaced %between% would any functionality in data.table package be affected? I think it shouldn't, [[ should work with atomic vector as [ does. Am I correct? thanks

> 3%between2%c(1,5)
[1] TRUE

Upvotes: 2

Views: 283

Answers (1)

Thomas
Thomas

Reputation: 44555

I thought this was an interesting question because I wondered how one might look, in general, for uses of a function by other functions. As far as I know, there's no way to directly do this (perhaps someone can correct me?). But, I put together a little code that looks for function names in text representations of other functions. For %between%, here's the following:

library(data.table)
objs <- objects("package:data.table")
z <- textConnection("funs", open="w")
dump(list=objs, file=z)
close(z)

# find all mentions of `%between%` in `data.table` functions
funs[grep("%between%",funs)] ## only mentions are when %between% is defined

# find all mentions of all `data.table` functions in `data.table` functions
locations <- lapply(objs, function(x) grep(x,funs))
names(locations) <- objs

UPDATE: After doing some more searching, this question/answer also seem to provide some more information on how to detect dependencies programmatically using foodweb from library(mvbutils).

Upvotes: 3

Related Questions