This commit is contained in:
Tom Elliott 2025-01-10 09:20:28 +13:00
parent 9be1c72a03
commit 57cdc92a2a
2 changed files with 35 additions and 17 deletions

View File

@ -1,22 +1,9 @@
# overload input/return types # overload input/return types
sample_num <- ts_function(
sample,
x = ts_numeric(0),
result = ts_numeric(1)
)
sampler <- ts_function(
function() {
list(
sample_one = sample_num(0)
)
},
result = ts_list(
num = sample_num
)
)
ts_compile(d_normal)
# ts_compile(d_normal)
# compile to: # compile to:
# const sampler = R.ocap( # const sampler = R.ocap(

View File

@ -1,7 +1,7 @@
# # optional, check arguments - useful for debugging/development # # optional, check arguments - useful for debugging/development
# check_args(match.call(), formals()) # check_args(match.call(), formals())
test_that("function definitions", { test_that("anonomous function definitions", {
add <- ts_function( add <- ts_function(
function(a = ts_numeric(1), b = ts_numeric(1)) a + b, function(a = ts_numeric(1), b = ts_numeric(1)) a + b,
result = ts_numeric(1) result = ts_numeric(1)
@ -10,3 +10,34 @@ test_that("function definitions", {
expect_equal(add(1, 2), 3) expect_equal(add(1, 2), 3)
expect_error(add("a", 2)) expect_error(add("a", 2))
}) })
test_that("named function definitions", {
sample_num <- ts_function(
sample,
x = ts_numeric(),
result = ts_numeric()
)
x <- sample_num(1:10)
expect_true(all(x %in% 1:10))
expect_error(sample_num("a"))
})
test_that("function with complex return types", {
sampler <- ts_function(
function(x = ts_numeric()) {
list(
get = function(n) sample(x, n)
)
},
result = ts_list(
get = ts_function(
NULL,
n = ts_integer(1),
result = ts_numeric(1)
)
)
)
s <- sampler(1:10)
})