Skip to content

Commit

Permalink
feat-fix: function argument position forced by mapply (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce authored Sep 9, 2024
2 parents 1d50d8f + d86b7bb commit 953465d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/dataflow/environments/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ registerSimpleFunctions(
'do.call', 'rbind', 'nrow', 'ncol', 'tryCatch', 'expression', 'factor',
'missing', 'as.data.frame', 'data.frame', 'na.omit', 'rownames', 'names', 'order', 'length', 'any', 'dim', 'matrix', 'cbind', 'nchar', 't'
)
registerBuiltInFunctions(false, ['lapply', 'sapply', 'vapply', 'mapply'], processApply, { indexOfFunction: 1, nameOfFunctionArgument: 'FUN' } )
registerBuiltInFunctions(false, ['mapply', 'Mapply'], processApply, { indexOfFunction: 0, nameOfFunctionArgument: 'FUN' } )
registerBuiltInFunctions(false, ['lapply', 'sapply', 'vapply'], processApply, { indexOfFunction: 1, nameOfFunctionArgument: 'FUN' } )
/* functool wrappers */
registerBuiltInFunctions(false, ['Lapply', 'Sapply', 'Vapply', 'Mapply'], processApply, { indexOfFunction: 1, nameOfFunctionArgument: 'FUN' } )
registerBuiltInFunctions(false, ['Lapply', 'Sapply', 'Vapply'], processApply, { indexOfFunction: 1, nameOfFunctionArgument: 'FUN' } )
registerBuiltInFunctions(false, ['apply', 'tapply', 'Tapply'], processApply, { indexOfFunction: 2, nameOfFunctionArgument: 'FUN' } )
registerBuiltInFunctions(false, ['print'], defaultBuiltInProcessor, { returnsNthArgument: 0, forceArgs: 'all' as const } )
registerBuiltInFunctions(true, ['('], defaultBuiltInProcessor, { returnsNthArgument: 0 } )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ a(,3)`, emptyGraph()
describe('*apply', () => {
const caps: SupportedFlowrCapabilityId[] = ['function-calls', 'unnamed-arguments', 'formals-named', 'function-definitions', 'numbers', 'name-normal', ...OperatorDatabase['<-'].capabilities, ...OperatorDatabase['*'].capabilities]
describe('no additional arguments', () => {
for(const applyFn of ['sapply', 'vapply', 'mapply', 'lapply']) {
for(const applyFn of ['sapply', 'vapply', 'lapply']) {
assertDataflow(label(applyFn + ' without arguments', caps), shell, `g <- function(x) { x * 2 }
x <- 1:3
${applyFn}(x, g)`,
Expand All @@ -390,7 +390,7 @@ ${applyFn}(x, g)`,
}
})
describe('with additional arguments', () => {
for(const applyFn of ['sapply', 'vapply', 'mapply']) {
for(const applyFn of ['sapply', 'vapply']) {
assertDataflow(label(applyFn + ' with arguments', caps), shell, `g <- function(x, y, z, a) { x * 2 }
x <- 1:3
k <- 5
Expand Down
42 changes: 29 additions & 13 deletions test/functionality/slicing/static-program-slices/calls-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,19 +629,35 @@ if(x == 3) {
{ x <- y <- 3 }
x`)
})
describe('Lapply Forcing the Map Function Body', () => {
assertSliced(label('Forcing Second Argument', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'res <- lapply(1:3, function(x) x + 1)', ['1@res'],
'res <- lapply(1:3, function(x) x + 1)'
)
assertSliced(label('Forcing Including Reference', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)', ['2@res'],
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)'
)
describe('Apply Functions', () => {
describe('Lapply Forcing the Map Function Body', () => {
assertSliced(label('Forcing Second Argument', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'res <- lapply(1:3, function(x) x + 1)', ['1@res'],
'res <- lapply(1:3, function(x) x + 1)'
)
assertSliced(label('Force-Including Reference', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)', ['2@res'],
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)'
)
})
describe('Mapply Forcing the Map Function Body in the first arg', () => {
assertSliced(label('Forcing First Argument', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'res <- mapply(function(x) x + 1, 1:3)', ['1@res'],
'res <- mapply(function(x) x + 1, 1:3)'
)
assertSliced(label('Force-Including Reference', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'foo <- bar()\nres <- mapply(function(x) foo * 2, 1:3)', ['2@res'],
'foo <- bar()\nres <- mapply(function(x) foo * 2, 1:3)'
)
})
})
describe('Using built-in names as a variable', () => {
for(const [loop, loopLabel] of [['for(i in 1:length(l))', 'for-loop'], ['while(xx)', 'while-loop'], ['repeat', 'repeat-loop']] as const) {
Expand Down

2 comments on commit 953465d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"artificial" Benchmark Suite

Benchmark suite Current: 953465d Previous: d6d97d3 Ratio
Retrieve AST from R code 235.211101 ms (99.1210749364181) 237.1307347272727 ms (99.33189983148677) 0.99
Normalize R AST 20.40247640909091 ms (36.109085925954645) 20.22688240909091 ms (34.81944475834837) 1.01
Produce dataflow information 38.06858240909091 ms (81.74126889637533) 38.76148281818182 ms (83.37589845299341) 0.98
Total per-file 800.1969117272728 ms (1421.526455004694) 805.4951149545454 ms (1418.989224836618) 0.99
Static slicing 2.2612167046408826 ms (1.1980402354216464) 2.2549209922472833 ms (1.3467440237997401) 1.00
Reconstruct code 0.2318122130157004 ms (0.194699941568812) 0.22440795860531199 ms (0.17245303864543157) 1.03
Total per-slice 2.509573199930101 ms (1.2525013104173244) 2.4968072479681567 ms (1.402430824638579) 1.01
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.7869360165281424 # 0.7869360165281424 # 1
reduction (normalized tokens) 0.7639690077689504 # 0.7639690077689504 # 1
memory (df-graph) 147.42458274147728 KiB (358.6827375397903) 147.42458274147728 KiB (358.6827375397903) 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"social-science" Benchmark Suite

Benchmark suite Current: 953465d Previous: d6d97d3 Ratio
Retrieve AST from R code 258.22234246 ms (50.2086650283636) 238.45832514 ms (44.01516969239197) 1.08
Normalize R AST 23.5938463 ms (17.554200110219313) 21.8018916 ms (16.153242755954302) 1.08
Produce dataflow information 79.938353 ms (95.19586954002874) 73.37089498 ms (86.87821424333349) 1.09
Total per-file 11177.679839879998 ms (53761.91113814251) 10697.70673224 ms (51757.12122600268) 1.04
Static slicing 21.642410590667605 ms (80.49385979362881) 20.852021200476752 ms (78.34298008513575) 1.04
Reconstruct code 0.29796199843971494 ms (0.17196463049752783) 0.22516315606876278 ms (0.13784797448770958) 1.32
Total per-slice 21.950445114532148 ms (80.5110505597717) 21.08480712275338 ms (78.36487556608114) 1.04
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.8959403739742829 # 0.8944619525615458 # 1.00
reduction (normalized tokens) 0.855704889245872 # 0.8534320485134076 # 1.00
memory (df-graph) 146.772421875 KiB (154.0026580992293) 146.770703125 KiB (154.0029022815246) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.