Created
May 31, 2022 14:45
-
-
Save nacnudus/82b97a9f6d42e153cd24b5f48ea5aabc to your computer and use it in GitHub Desktop.
Inspect a nested hashtab as a list
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
# Inspect a nested hash table as a list | |
as.list.hashtab <- function(h) { | |
val <- vector("list", 0) | |
maphash(h, function(k, v) { | |
r <- if (is.hashtab(v)) { | |
as.list.hashtab(v) | |
}else { | |
v | |
} | |
val[[k]] <<- r | |
}) | |
val | |
} | |
h <- hashtab() | |
sethash(h, 1, FALSE) | |
sethash(h, 2, hashtab()) | |
sethash(gethash(h, 2), "a", -1) | |
sethash(gethash(h, 2), "b", -2) | |
as.list(h) | |
# [[1]] | |
# [1] FALSE | |
# | |
# [[2]] | |
# [[2]]$bjkk | |
# [1] -2 | |
# | |
# [[2]]$a | |
# [1] -1 | |
# It fails when the key can't be used to name a list element. | |
h2 <- hashtab() | |
sethash(h2, new.env(), FALSE) | |
as.list(h2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment