3 if(!exists("..types..")) source("types.r")
4 if(!exists("..printer..")) source("printer.r")
9 pr_str
<- function(...)
10 .pr_list(list(...), print_readably
=TRUE, join
=" ")
13 .pr_list(list(...), print_readably
=FALSE, join
="")
15 prn
<- function(...) {
16 cat(.pr_list(list(...), print_readably
=TRUE, join
=" "))
21 println
<- function(...) {
22 cat(.pr_list(list(...), print_readably
=FALSE, join
=" "))
27 do_readline
<- function(prompt
) {
29 if (is
.null(l
)) nil
else l
33 do_get
<- function(hm
,k
) {
34 if (class(hm
) == "nil") return(nil
)
36 if (is
.null(v
)) nil
else v
38 contains_q
<-function(hm
,k
) {
39 if (class(hm
) == "nil") return(FALSE)
40 if (is
.null(hm
[[k
]])) FALSE else TRUE
44 cons
<- function(a
,b
) {
45 new_lst
<- append(list(a
), b
)
49 nth
<- function(a
,b
) {
53 throw("nth: index out of range")
56 do_concat
<- function(...) {
59 new_lst
<- append(new_lst
, l
)
64 do_apply
<- function(f
, ...) {
68 for(l
in slice(p
, 1, length(p
)-1)) {
69 args
[[length(args
)+1]] <- l
72 args
<- append(args
, p
[[length(p
)]])
76 map
<- function(f
, seq
) {
77 new
.listl(lapply(seq
, function(el
) fapply(f
, list(el
))))
80 conj
<- function(obj
, ...) {
82 new_obj
<- .clone(obj
)
85 for(l
in p
) new_obj
<- append(list(l
), new_obj
)
88 } else if (.vector_q(obj
)) {
90 for(l
in p
) new_obj
<- append(new_obj
, list(l
))
94 throw("conj called on non-sequence")
98 do_seq
<- function(obj
) {
100 if (length(obj
) == 0) nil
else obj
101 } else if (.vector_q(obj
)) {
102 if (length(obj
) == 0) nil
else new
.listl(.clone(obj
))
103 } else if (.string_q(obj
)) {
104 if (nchar(obj
) == 0) nil
else new
.listl(strsplit(obj
, "")[[1]])
105 } else if (class(obj
) == "nil") {
108 throw("seq: called on non-sequence")
114 with_meta
<- function(obj
, m
) {
115 new_obj
<- .clone(obj
)
116 attr(new_obj
, "meta") <- m
120 meta
<- function(obj
) {
121 m
<- attr(obj
, "meta")
122 if (is
.null(m
)) nil
else m
126 deref
<- function(atm
) atm$val
127 reset_bang
<- function (atm
, val
) { atm$val
<- val
; val
}
128 swap_bang
<- function (atm
, f
, ...) {
130 args
<- list(atm$val
)
132 for(l
in p
) args
[[length(args
)+1]] <- l
134 atm$val
<- fapply(f
, args
)
138 "="=function(a
,b
) .equal_q(a
,b
),
139 "throw"=function(err
) throw(err
),
146 "keyword"=new
.keyword
,
147 "keyword?"=.keyword_q
,
156 "readline"=do_readline
,
157 "read-string"=function(str
) read_str(str
),
158 "slurp"=function(path
) readChar(path
, file
.info(path
)$size
),
159 "<"=function(a
,b
) a
<b
,
160 "<="=function(a
,b
) a
<=b
,
161 ">"=function(a
,b
) a
>b
,
162 ">="=function(a
,b
) a
>=b
,
163 "+"=function(a
,b
) a
+b
,
164 "-"=function(a
,b
) a
-b
,
165 "*"=function(a
,b
) a
*b
,
166 "/"=function(a
,b
) a
/b
,
167 "time-ms"=function() round(as
.numeric(Sys
.time())*1000),
170 "list?"=function(a
) .list_q(a
),
172 "vector?"=function(a
) .vector_q(a
),
173 "hash-map"=new
.hash_map
,
174 "map?"=function(a
) .hash_map_q(a
),
175 "assoc"=function(hm
,...) .assoc(hm
,list(...)),
176 "dissoc"=function(hm
,...) .dissoc(hm
,list(...)),
178 "contains?"=contains_q
,
179 "keys"=function(hm
) new
.listl(ls(hm
)),
180 "vals"=function(hm
) new
.listl(lapply(ls(hm
), function(x
) hm
[[x
]])),
182 "sequential?"=.sequential_q
,
186 "first"=function(a
) if (.nil_q(a
) || length(a
) < 1) nil
else a
[[1]],
187 "rest"=function(a
) if (.nil_q(a
)) new
.list() else new
.listl(slice(a
,2)),
188 "empty?"=function(a
) .sequential_q(a
) && length(a
) == 0,
189 "count"=function(a
) if (.nil_q(a
)) 0 else length(a
),
196 "with-meta"=with_meta
,