Created
April 30, 2016 22:27
-
-
Save jeremyrsellars/677afb332efa6dcd447b1be33e01f364 to your computer and use it in GitHub Desktop.
R-clojure socket repl interop experiments
This file contains hidden or 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
# java -cp \clojure-1.8.0\clojure-1.8.0.jar -Dclojure.server.repl="{:port 5555 :accept clojure.core.server/repl :bind-err false}" clojure.main | |
# I had to change keyprefix/suffix from looking like a keyword | |
# to being a string because keywords don't support spaces well. | |
# Original: | |
## key_prefix <- ":" | |
## key_suffix <- "" | |
# Todo: Technically, might want to escape quotes in "named lists". | |
# Todo: Add fromEDN. | |
#BEGIN https://github.com/keminglabs/c2po-r/blob/master/R/edn.r | |
df_to_list <- function(ds) apply(ds, 1, function(x) as.list(x)) | |
map_kv_separator <- " " | |
map_item_separator <- " " | |
vector_item_separator <- " " | |
key_prefix <- "\"" | |
key_suffix <- "\"" | |
toEDN <- function(x){ | |
#convert factors to characters | |
if( is.factor( x ) == TRUE ) { | |
tmp_names <- names( x ) | |
x = as.character( x ) | |
names( x ) <- tmp_names | |
} | |
#convert data frames to list of lists | |
if( is.data.frame(x) ) { | |
x <- df_to_list( x ) | |
} | |
#convert quoted things to keywords | |
if( is.name(x) ) { | |
return( paste(":", x, sep="") ) | |
} | |
if( !is.vector(x) && !is.null(x) && !is.list(x) ) { | |
x <- as.list( x ) | |
warning("EDN only supports vectors and lists - But I'll try anyways") | |
} | |
if( is.null(x) ) | |
return( "null" ) | |
#treat named vectors as lists | |
if( is.null( names( x ) ) == FALSE ) { | |
x <- as.list( x ) | |
} | |
#named lists only | |
if( is.list(x) && !is.null(names(x)) ) { | |
if( any(duplicated(names(x))) ) | |
stop( "An EDN list must have unique names" ); | |
str = "{" | |
first_elem = TRUE | |
for( n in names(x) ) { | |
if( first_elem ) | |
first_elem = FALSE | |
else | |
str = paste(str, map_item_separator, sep="") | |
str = paste(str, key_prefix, n, key_suffix, map_kv_separator, toEDN(x[[n]]), sep="") | |
} | |
str = paste( str, "}", sep="" ) | |
return( str ) | |
} | |
#treat lists without names as a vector | |
if( length(x) != 1 || is.list(x) ) { | |
if( !is.null(names(x)) ) | |
return( toEDN(as.list(x)) ) #vector with names - treat as vector | |
str = "[" | |
first_elem = TRUE | |
for( val in x ) { | |
if( first_elem ) | |
first_elem = FALSE | |
else | |
str = paste(str, vector_item_separator, sep="") | |
str = paste(str, toEDN(val), sep="") | |
} | |
str = paste( str, "]", sep="" ) | |
return( str ) | |
} | |
if( is.nan(x) ) | |
return( "\"NaN\"" ) | |
if( is.na(x) ) | |
return( "\"NA\"" ) | |
if( is.infinite(x) ) | |
return( ifelse( x == Inf, "\"Inf\"", "\"-Inf\"" ) ) | |
if( is.logical(x) ) | |
return( ifelse(x, "true", "false") ) | |
if( is.character(x) ) | |
return( gsub("\\/", "\\\\/", deparse(x)) ) | |
if( is.numeric(x) ) | |
return( as.character(x) ) | |
stop( "shouldnt make it here - unhandled type not caught" ) | |
} | |
# END https://github.com/keminglabs/c2po-r/blob/master/R/edn.r | |
con <- socketConnection(host="localhost", 5555) | |
#open(con) | |
writeLines(c("(defn make-greeting [n]", | |
" (str \"Greetings, \\n \" n))", | |
")"), con) | |
writeLines(c("(make-greeting \"Jeremy\")"), con) | |
resp <- readLines(con) | |
writeLines(c("(clojure.pprint/pprint *1)"), con) | |
resp <- readLines(con) | |
resp | |
sub("([^=]+=>\\s*)+", "", resp, perl = T) | |
if(FALSE){ | |
writeLines(c("*1"), con) | |
writeLines(c("(clojure.pprint/pprint *1)"), con) | |
writeLines(c("(clojure.pprint/pprint ", toEDN(mtcars), ")"), con) | |
writeLines(c("(defn mtcars [] ", toEDN(mtcars), ")"), con) | |
writeLines(c("(clojure.pprint/pprint (mtcars))"), con) | |
writeLines(toEDN(mtcars), con) | |
paste0(resp, collapse = "\n") | |
writeLines(c("(clojure.pprint/pprint mtcars)"), con) | |
} | |
close(con) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment