jonthegeek / factory

factory: Build Function Factories
Other
49 stars 6 forks source link

Feature/state #35

Open TylerGrantSmith opened 4 years ago

TylerGrantSmith commented 4 years ago

Not quite there yet...

library(factory)
new_counter <- build_factory(function() { i <<- i + 1; i }, .env = rlang::child_env(.parent = .GlobalEnv, i = 0))
#> Warning in as.list(fn_body) == before: longer object length is not a multiple of
#> shorter object length

#> Warning in as.list(fn_body) == before: longer object length is not a multiple of
#> shorter object length
counter_one <- new_counter()
counter_two <- new_counter()
counter_one()
#> [1] 1
counter_one()
#> [1] 2
counter_two()
#> [1] 3
counter_one
#> function () 
#> {
#>     i <<- i + 1
#>     i
#> }
#> <environment: 0x00000000143b9f28>
#> attr(,"class")
#> [1] "stateful_function" "function"
counter_two
#> function () 
#> {
#>     i <<- i + 1
#>     i
#> }
#> <environment: 0x00000000143b9f28>
#> attr(,"class")
#> [1] "stateful_function" "function"

Created on 2020-06-03 by the reprex package (v0.3.0)

TylerGrantSmith commented 4 years ago

strange that printing the stateful_function doesn't reprex correctly.

Nevermind, it is because I didn't rebuild the documentation to catch the s3method

TylerGrantSmith commented 4 years ago

Originally I was thinking the user could just supply the .env but that has the unintended consequence of sharing the environment with all functions. maybe instead it should just provide a named list and the environment is built in the factory

TylerGrantSmith commented 4 years ago

Ok, I think it is ugly, but now build_factory takes a named list argument .state which, if provided will be used to make the manufactured functions environment a child environment of the calling environment with bindings taken from .state. I also modified body_insert to not produce warnings when length(before)>1

jonthegeek commented 4 years ago

Please remove the test on line 16 of test-build_factory.R

Also, please run styler::style_pkg() and goodpractice::gp(), and follow the advice in those.

jonthegeek commented 4 years ago

Closes #34.

TylerGrantSmith commented 4 years ago

The travis test is failing because of a warning that before is not documented on the roxygen code of body_insert. Why is this an exported function, btw?