bash, c, clojure, coffee, es6, js: add seq/string?
[jackhill/mal.git] / js / core.js
1 // Node vs browser behavior
2 var core = {};
3 if (typeof module === 'undefined') {
4 var exports = core;
5 } else {
6 var types = require('./types'),
7 readline = require('./node_readline'),
8 reader = require('./reader'),
9 printer = require('./printer');
10 }
11
12 // Errors/Exceptions
13 function mal_throw(exc) { throw exc; }
14
15
16 // String functions
17 function pr_str() {
18 return Array.prototype.map.call(arguments,function(exp) {
19 return printer._pr_str(exp, true);
20 }).join(" ");
21 }
22
23 function str() {
24 return Array.prototype.map.call(arguments,function(exp) {
25 return printer._pr_str(exp, false);
26 }).join("");
27 }
28
29 function prn() {
30 printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) {
31 return printer._pr_str(exp, true);
32 }));
33 }
34
35 function println() {
36 printer.println.apply({}, Array.prototype.map.call(arguments,function(exp) {
37 return printer._pr_str(exp, false);
38 }));
39 }
40
41 function slurp(f) {
42 if (typeof require !== 'undefined') {
43 return require('fs').readFileSync(f, 'utf-8');
44 } else {
45 var req = new XMLHttpRequest();
46 req.open("GET", f, false);
47 req.send();
48 if (req.status == 200) {
49 return req.responseText;
50 } else {
51 throw new Error("Failed to slurp file: " + f);
52 }
53 }
54 }
55
56
57 // Number functions
58 function time_ms() { return new Date().getTime(); }
59
60
61 // Hash Map functions
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);
66 }
67
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);
72 }
73
74 function get(hm, key) {
75 if (hm != null && key in hm) {
76 return hm[key];
77 } else {
78 return null;
79 }
80 }
81
82 function contains_Q(hm, key) {
83 if (key in hm) { return true; } else { return false; }
84 }
85
86 function keys(hm) { return Object.keys(hm); }
87 function vals(hm) { return Object.keys(hm).map(function(k) { return hm[k]; }); }
88
89
90 // Sequence functions
91 function cons(a, b) { return [a].concat(b); }
92
93 function concat(lst) {
94 lst = lst || [];
95 return lst.concat.apply(lst, Array.prototype.slice.call(arguments, 1));
96 }
97
98 function nth(lst, idx) {
99 if (idx < lst.length) { return lst[idx]; }
100 else { throw new Error("nth: index out of range"); }
101 }
102
103 function first(lst) { return (lst === null) ? null : lst[0]; }
104
105 function rest(lst) { return (lst == null) ? [] : lst.slice(1); }
106
107 function empty_Q(lst) { return lst.length === 0; }
108
109 function count(s) {
110 if (Array.isArray(s)) { return s.length; }
111 else if (s === null) { return 0; }
112 else { return Object.keys(s).length; }
113 }
114
115 function conj(lst) {
116 if (types._list_Q(lst)) {
117 return Array.prototype.slice.call(arguments, 1).reverse().concat(lst);
118 } else {
119 var v = lst.concat(Array.prototype.slice.call(arguments, 1));
120 v.__isvector__ = true;
121 return v;
122 }
123 }
124
125 function seq(obj) {
126 if (types._list_Q(obj)) {
127 return obj.length > 0 ? obj : null;
128 } else if (types._vector_Q(obj)) {
129 return obj.length > 0 ? Array.prototype.slice.call(obj, 0): null;
130 } else if (types._string_Q(obj)) {
131 return obj.length > 0 ? obj.split('') : null;
132 } else if (obj === null) {
133 return null;
134 } else {
135 throw new Error("seq: called on non-sequence");
136 }
137 }
138
139
140 function apply(f) {
141 var args = Array.prototype.slice.call(arguments, 1);
142 return f.apply(f, args.slice(0, args.length-1).concat(args[args.length-1]));
143 }
144
145 function map(f, lst) {
146 return lst.map(function(el){ return f(el); });
147 }
148
149
150 // Metadata functions
151 function with_meta(obj, m) {
152 var new_obj = types._clone(obj);
153 new_obj.__meta__ = m;
154 return new_obj;
155 }
156
157 function meta(obj) {
158 // TODO: support symbols and atoms
159 if ((!types._sequential_Q(obj)) &&
160 (!(types._hash_map_Q(obj))) &&
161 (!(types._function_Q(obj)))) {
162 throw new Error("attempt to get metadata from: " + types._obj_type(obj));
163 }
164 return obj.__meta__;
165 }
166
167
168 // Atom functions
169 function deref(atm) { return atm.val; }
170 function reset_BANG(atm, val) { return atm.val = val; }
171 function swap_BANG(atm, f) {
172 var args = [atm.val].concat(Array.prototype.slice.call(arguments, 2));
173 atm.val = f.apply(f, args);
174 return atm.val;
175 }
176
177
178 // types.ns is namespace of type functions
179 var ns = {'type': types._obj_type,
180 '=': types._equal_Q,
181 'throw': mal_throw,
182 'nil?': types._nil_Q,
183 'true?': types._true_Q,
184 'false?': types._false_Q,
185 'string?': types._string_Q,
186 'symbol': types._symbol,
187 'symbol?': types._symbol_Q,
188 'keyword': types._keyword,
189 'keyword?': types._keyword_Q,
190
191 'pr-str': pr_str,
192 'str': str,
193 'prn': prn,
194 'println': println,
195 'readline': readline.readline,
196 'read-string': reader.read_str,
197 'slurp': slurp,
198 '<' : function(a,b){return a<b;},
199 '<=' : function(a,b){return a<=b;},
200 '>' : function(a,b){return a>b;},
201 '>=' : function(a,b){return a>=b;},
202 '+' : function(a,b){return a+b;},
203 '-' : function(a,b){return a-b;},
204 '*' : function(a,b){return a*b;},
205 '/' : function(a,b){return a/b;},
206 "time-ms": time_ms,
207
208 'list': types._list,
209 'list?': types._list_Q,
210 'vector': types._vector,
211 'vector?': types._vector_Q,
212 'hash-map': types._hash_map,
213 'map?': types._hash_map_Q,
214 'assoc': assoc,
215 'dissoc': dissoc,
216 'get': get,
217 'contains?': contains_Q,
218 'keys': keys,
219 'vals': vals,
220
221 'sequential?': types._sequential_Q,
222 'cons': cons,
223 'concat': concat,
224 'nth': nth,
225 'first': first,
226 'rest': rest,
227 'empty?': empty_Q,
228 'count': count,
229 'apply': apply,
230 'map': map,
231
232 'conj': conj,
233 'seq': seq,
234
235 'with-meta': with_meta,
236 'meta': meta,
237 'atom': types._atom,
238 'atom?': types._atom_Q,
239 "deref": deref,
240 "reset!": reset_BANG,
241 "swap!": swap_BANG};
242
243 exports.ns = core.ns = ns;