From 027b2d339ee96af4d4c3d4e184d30e9404859fdf Mon Sep 17 00:00:00 2001 From: Zebulun Arendsee Date: Mon, 19 Feb 2018 00:56:19 -0600 Subject: [PATCH] Add language specialization example in R --- examples/types/by-language/types.R | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/examples/types/by-language/types.R b/examples/types/by-language/types.R index 3c316880..74524958 100644 --- a/examples/types/by-language/types.R +++ b/examples/types/by-language/types.R @@ -19,3 +19,33 @@ f <- function(x) x + 1 f <- function(x){ colSums(x) } + + +## Two different language specific specializations of Table +#-- RDataFrame :: +#-- lang "R" +#-- cast_as "data.frame" +#-- is_a Table +#-- RDataTable :: +#-- lang "R" +#-- cast_as "data.table" +#-- import "data.table" +#-- is_a Table + +## Define the casting function +#-- as.data.table :: role "cast" => RDataFrame a -> RDataTable a +#-- as.data.frame :: role "cast" => RDataTable a -> RDataFrame a + +## Finite set of symbols (Enum) but encoded in R as a string (rather than the +## default, a factor). We avoid the issues of handling factors, but retain the +## semantic information. +#-- RStringFactor :: +#-- lang "R" +#-- cast_as "character" +#-- is_a Enum + +#-- foo :: RDataFrame {a:Integer; b:String; c:RStringFactor (Cat, Dog, Eel)} -> +#-- RDataFrame {a:Integer; b:String} +foo <- function(x){ + subset(x, c == "Eel")[-3] +}