1 // Node vs browser behavior
3 if (typeof module
=== 'undefined') {
6 var types
= require('./types'),
7 readline
= require('./node_readline'),
8 reader
= require('./reader'),
9 printer
= require('./printer');
13 function mal_throw(exc
) { throw exc
; }
18 return Array
.prototype.map
.call(arguments
,function(exp
) {
19 return printer
._pr_str(exp
, true);
24 return Array
.prototype.map
.call(arguments
,function(exp
) {
25 return printer
._pr_str(exp
, false);
30 printer
.println
.apply({}, Array
.prototype.map
.call(arguments
,function(exp
) {
31 return printer
._pr_str(exp
, true);
36 printer
.println
.apply({}, Array
.prototype.map
.call(arguments
,function(exp
) {
37 return printer
._pr_str(exp
, false);
42 if (typeof require
!== 'undefined') {
43 return require('fs').readFileSync(f
, 'utf-8');
45 var req
= new XMLHttpRequest();
46 req
.open("GET", f
, false);
48 if (req
.status
== 200) {
49 return req
.responseText
;
51 throw new Error("Failed to slurp file: " + f
);
58 function time_ms() { return new Date().getTime(); }
62 function assoc(src_hm
) {
63 var hm
= types
._clone(src_hm
);
64 var args
= [hm
].concat(Array
.prototype.slice
.call(arguments
, 1));
65 return types
._assoc_BANG
.apply(null, args
);
68 function dissoc(src_hm
) {
69 var hm
= types
._clone(src_hm
);
70 var args
= [hm
].concat(Array
.prototype.slice
.call(arguments
, 1));
71 return types
._dissoc_BANG
.apply(null, args
);
74 function get(hm
, key
) {
75 if (hm
!= null && key
in hm
) {
82 function contains_Q(hm
, key
) {
83 if (key
in hm
) { return true; } else { return false; }
86 function keys(hm
) { return Object
.keys(hm
); }
87 function vals(hm
) { return Object
.keys(hm
).map(function(k
) { return hm
[k
]; }); }
91 function cons(a
, b
) { return [a
].concat(b
); }
93 function concat(lst
) {
95 return lst
.concat
.apply(lst
, Array
.prototype.slice
.call(arguments
, 1));
98 function nth(lst
, idx
) {
99 if (idx
< lst
.length
) { return lst
[idx
]; }
100 else { throw new Error("nth: index out of range"); }
103 function first(lst
) { return lst
[0]; }
105 function rest(lst
) { return lst
.slice(1); }
107 function empty_Q(lst
) { return lst
.length
=== 0; }
110 if (Array
.isArray(s
)) { return s
.length
; }
111 else if (s
=== null) { return 0; }
112 else { return Object
.keys(s
).length
; }
116 if (types
._list_Q(lst
)) {
117 return Array
.prototype.slice
.call(arguments
, 1).reverse().concat(lst
);
119 var v
= lst
.concat(Array
.prototype.slice
.call(arguments
, 1));
120 v
.__isvector__
= true;
126 var args
= Array
.prototype.slice
.call(arguments
, 1);
127 return f
.apply(f
, args
.slice(0, args
.length
-1).concat(args
[args
.length
-1]));
130 function map(f
, lst
) {
131 return lst
.map(function(el
){ return f(el
); });
135 // Metadata functions
136 function with_meta(obj
, m
) {
137 var new_obj
= types
._clone(obj
);
138 new_obj
.__meta__
= m
;
143 // TODO: support symbols and atoms
144 if ((!types
._sequential_Q(obj
)) &&
145 (!(types
._hash_map_Q(obj
))) &&
146 (!(types
._function_Q(obj
)))) {
147 throw new Error("attempt to get metadata from: " + types
._obj_type(obj
));
154 function deref(atm
) { return atm
.val
; }
155 function reset_BANG(atm
, val
) { return atm
.val
= val
; }
156 function swap_BANG(atm
, f
) {
157 var args
= [atm
.val
].concat(Array
.prototype.slice
.call(arguments
, 2));
158 atm
.val
= f
.apply(f
, args
);
163 // types.ns is namespace of type functions
164 var ns
= {'type': types
._obj_type
,
167 'nil?': types
._nil_Q
,
168 'true?': types
._true_Q
,
169 'false?': types
._false_Q
,
170 'symbol': types
._symbol
,
171 'symbol?': types
._symbol_Q
,
172 'keyword': types
._keyword
,
173 'keyword?': types
._keyword_Q
,
179 'readline': readline
.readline
,
180 'read-string': reader
.read_str
,
182 '<' : function(a
,b
){return a
<b
;},
183 '<=' : function(a
,b
){return a
<=b
;},
184 '>' : function(a
,b
){return a
>b
;},
185 '>=' : function(a
,b
){return a
>=b
;},
186 '+' : function(a
,b
){return a
+b
;},
187 '-' : function(a
,b
){return a
-b
;},
188 '*' : function(a
,b
){return a
*b
;},
189 '/' : function(a
,b
){return a
/b
;},
193 'list?': types
._list_Q
,
194 'vector': types
._vector
,
195 'vector?': types
._vector_Q
,
196 'hash-map': types
._hash_map
,
197 'map?': types
._hash_map_Q
,
201 'contains?': contains_Q
,
205 'sequential?': types
._sequential_Q
,
217 'with-meta': with_meta
,
220 'atom?': types
._atom_Q
,
222 "reset!": reset_BANG
,
225 exports
.ns
= core
.ns
= ns
;