-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ad19c0
commit 5fe17e4
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |