aboutsummaryrefslogtreecommitdiff
path: root/r/types.r
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-11-03 21:19:13 -0600
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:44 -0600
commit36737ae57ee106be16426bf9ef088380421ca0ec (patch)
treed3c405f95ccf63ab3897a4f17566bcc149cd7bf3 /r/types.r
parent8128c69a1da03d3b8d62defb4533c3e3ae706682 (diff)
downloadmal-36737ae57ee106be16426bf9ef088380421ca0ec.tar.gz
mal-36737ae57ee106be16426bf9ef088380421ca0ec.zip
R: add hash-map and metadata support.
Diffstat (limited to 'r/types.r')
-rw-r--r--r/types.r60
1 files changed, 42 insertions, 18 deletions
diff --git a/r/types.r b/r/types.r
index 98047eb..06fe5a7 100644
--- a/r/types.r
+++ b/r/types.r
@@ -45,6 +45,17 @@ slice <- function(seq, start=1, end=-1) {
})
}
+.clone <- function(obj) {
+ if (.hash_map_q(obj)) {
+ new_obj <- new.env()
+ for(k in ls(obj, all.names=TRUE)) new_obj[[k]] = obj[[k]]
+ class(new_obj) <- "HashMap"
+ } else {
+ new_obj <- obj
+ }
+ new_obj
+}
+
# Errors/exceptions
thrown_error = new.env()
thrown_error$val = NULL
@@ -96,26 +107,39 @@ fapply <- function(mf, args) {
}
# Lists
-new.list <- function(...) {
- lst <- list(...)
- class(lst) <- "List"
- lst
-}
-new.listl <- function(lst) {
- class(lst) <- "List"
- lst
-}
+new.list <- function(...) new.listl(list(...))
+new.listl <- function(lst) { class(lst) <- "List"; lst }
.list_q <- function(obj) "List" == class(obj)
# Vectors
-new.vector <- function(...) {
- lst <- list(...)
- class(lst) <- "Vector"
- lst
-}
-new.vectorl <- function(lst) {
- class(lst) <- "Vector"
- lst
-}
+new.vector <- function(...) new.vectorl(list(...))
+new.vectorl <- function(lst) { class(lst) <- "Vector"; lst }
.vector_q <- function(obj) "Vector" == class(obj)
+# Hash Maps
+new.hash_map <- function(...) new.hash_mapl(list(...))
+new.hash_mapl <- function(lst) {
+ .assoc(new.env(), lst)
+}
+.assoc <- function(src_hm, lst) {
+ hm <- .clone(src_hm)
+ if (length(lst) > 0) {
+ for(i in seq(1,length(lst),2)) {
+ hm[[lst[[i]]]] <- lst[[i+1]]
+ }
+ }
+ class(hm) <- "HashMap"
+ hm
+}
+.dissoc <- function(src_hm, lst) {
+ hm <- .clone(src_hm)
+ if (length(lst) > 0) {
+ for(k in lst) {
+ remove(list=c(k), envir=hm)
+ }
+ }
+ ls(hm)
+ class(hm) <- "HashMap"
+ hm
+}
+.hash_map_q <- function(obj) "HashMap" == class(obj)