Skip to content

Commit

Permalink
Enum research inplace type.frame.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jul 29, 2024
1 parent 9ad19c0 commit 5fe17e4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
48 changes: 48 additions & 0 deletions dev/issues/14-enum-in-type.frame/test.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
box::use(dt = data.table)
# box::use(interface[type.frame, enum])

# Now create the PlayerFrame with an in-place enum declaration for 'shoots'
PlayerFrame <- type.frame(
frame = dt$data.table,
col_types = list(
name = character,
jersey_number = integer,
position = character,
birth_date = character,
age = integer,
birthplace = character,
nationality = character,
shoots = enum("Left", "Right"),
height_imperial = numeric,
height_metric = numeric,
weight_imperial = numeric,
weight_metric = numeric
)
)

# Now create the player_frame
player_frame <- PlayerFrame(
name = "John Doe",
jersey_number = 42L,
position = "Center",
birth_date = "January 1, 2000",
age = 21L,
birthplace = "Toronto, ON, CAN",
nationality = "CAN",
shoots = "Left",
height_imperial = 6.0,
height_metric = 183,
weight_imperial = 200,
weight_metric = 91
)

# Print the player_frame to verify
print(player_frame)

class(player_frame$shoots)

player_frame$shoots

class(player_frame$shoots)
class(player_frame$shoots)
str(player_frame$shoots)
31 changes: 31 additions & 0 deletions dev/wrap-user-fun-in-all.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
modify_user_function <- function(user_fun) {
body(user_fun) <- bquote({
all(.(body(user_fun)))
})
return(user_fun)
}

# Example user functions
example_fun1 <- function(value) value > 0
example_fun2 <- function(x) x %% 2 == 0
example_fun3 <- function(y) y >= 10 & y <= 20

# Modify the functions
modified_fun1 <- modify_user_function(example_fun1)
modified_fun2 <- modify_user_function(example_fun2)
modified_fun3 <- modify_user_function(example_fun3)

# unmodified functions; returns logical vectors
print(example_fun1(c(-1, 0, 1, 2)))
print(example_fun2(c(2, 4, 6, 8)))
print(example_fun3(c(5, 15, 25)))

# Test the modified functions
print(modified_fun1(c(-1, 0, 1, 2))) # FALSE
print(modified_fun2(c(2, 4, 6, 8))) # TRUE
print(modified_fun3(c(5, 15, 25))) # FALSE

# They still work with single values too
print(modified_fun1(5)) # TRUE
print(modified_fun2(3)) # FALSE
print(modified_fun3(15)) # TRUE

0 comments on commit 5fe17e4

Please sign in to comment.