Merge pull request #406 from chr15m/lib-alias-hacks
[jackhill/mal.git] / matlab / core.m
1 classdef core
2 methods(Static)
3 function ret = throw(obj)
4 ret = type_utils.nil;
5 if exist('OCTAVE_VERSION', 'builtin') ~= 0
6 % Until Octave has MException objects, we need to
7 % store the error object globally to be able to pass
8 % it to the error handler.
9 global error_object;
10 error_object = obj;
11 exc = struct('identifier', 'MalException:object',...
12 'message', 'MalException');
13 rethrow(exc);
14 else
15 throw(types.MalException(obj));
16 end
17 end
18
19 function str = pr_str(varargin)
20 strs = cellfun(@(s) printer.pr_str(s,true), varargin, ...
21 'UniformOutput', false);
22 str = strjoin(strs, ' ');
23 end
24 function str = do_str(varargin)
25 strs = cellfun(@(s) printer.pr_str(s,false), varargin, ...
26 'UniformOutput', false);
27 str = strjoin(strs, '');
28 end
29 function ret = prn(varargin)
30 strs = cellfun(@(s) printer.pr_str(s,true), varargin, ...
31 'UniformOutput', false);
32 fprintf('%s\n', strjoin(strs, ' '));
33 ret = type_utils.nil;
34 end
35 function ret = println(varargin)
36 strs = cellfun(@(s) printer.pr_str(s,false), varargin, ...
37 'UniformOutput', false);
38 fprintf('%s\n', strjoin(strs, ' '));
39 ret = type_utils.nil;
40 end
41
42 function ret = time_ms()
43 secs = now-repmat(datenum('1970-1-1 00:00:00'),size(now));
44 ret = floor(secs.*repmat(24*3600.0*1000,size(now)));
45 end
46
47 function new_hm = assoc(hm, varargin)
48 new_hm = clone(hm);
49 for i=1:2:length(varargin)
50 new_hm.set(varargin{i}, varargin{i+1});
51 end
52 end
53
54 function new_hm = dissoc(hm, varargin)
55 new_hm = clone(hm);
56 ks = intersect(hm.keys(),varargin);
57 if exist('OCTAVE_VERSION', 'builtin') ~= 0
58 new_hm.data.remove(ks);
59 else
60 remove(new_hm.data, ks);
61 end
62 end
63
64 function ret = get(hm, key)
65 if isa(hm, 'types.Nil')
66 ret = type_utils.nil;
67 elseif hm.data.isKey(key)
68 ret = hm.data(key);
69 else
70 ret = type_utils.nil;
71 end
72 end
73
74 function ret = keys(hm)
75 ks = hm.keys();
76 ret = types.List(ks{:});
77 end
78
79 function ret = vals(hm)
80 vs = hm.values();
81 ret = types.List(vs{:});
82 end
83
84 function ret = cons(a, seq)
85 cella = [{a}, seq.data];
86 ret = types.List(cella{:});
87 end
88
89 function ret = concat(varargin)
90 if nargin == 0
91 cella = {};
92 else
93 cells = cellfun(@(x) x.data, varargin, ...
94 'UniformOutput', false);
95 cella = cat(2,cells{:});
96 end
97 ret = types.List(cella{:});
98 end
99
100 function ret = first(seq)
101 if isa(seq, 'types.Nil')
102 ret = type_utils.nil;
103 elseif length(seq) < 1
104 ret = type_utils.nil;
105 else
106 ret = seq.get(1);
107 end
108 end
109
110 function ret = rest(seq)
111 if isa(seq, 'types.Nil')
112 ret = types.List();
113 else
114 cella = seq.data(2:end);
115 ret = types.List(cella{:});
116 end
117 end
118
119 function ret = nth(seq, idx)
120 if idx+1 > length(seq)
121 if exist('OCTAVE_VERSION', 'builtin') ~= 0
122 error('Range:nth', ...
123 'nth: index out of range');
124 else
125 throw(MException('Range:nth', ...
126 'nth: index out of range'))
127 end
128 end
129 ret = seq.get(idx+1);
130 end
131
132 function ret = apply(varargin)
133 f = varargin{1};
134 if isa(f, 'types.Function')
135 f = f.fn;
136 end
137 first_args = varargin(2:end-1);
138 rest_args = varargin{end}.data;
139 args = [first_args rest_args];
140 ret = f(args{:});
141 end
142
143 function ret = map(f, lst)
144 if isa(f, 'types.Function')
145 f = f.fn;
146 end
147 cells = cellfun(@(x) f(x), lst.data, 'UniformOutput', false);
148 ret = types.List(cells{:});
149 end
150
151 function ret = conj(varargin)
152 seq = varargin{1};
153 args = varargin(2:end);
154 if type_utils.list_Q(seq)
155 cella = [fliplr(args), seq.data];
156 ret = types.List(cella{:});
157 else
158 cella = [seq.data, args];
159 ret = types.Vector(cella{:});
160 end
161 end
162
163 function ret = seq(obj)
164 if type_utils.list_Q(obj)
165 if length(obj) > 0
166 ret = obj;
167 else
168 ret = type_utils.nil;
169 end
170 elseif type_utils.vector_Q(obj)
171 if length(obj) > 0
172 ret = types.List(obj.data{:});
173 else
174 ret = type_utils.nil;
175 end
176 elseif type_utils.string_Q(obj)
177 if length(obj) > 0
178 cells = cellfun(@(c) char(c),...
179 num2cell(double(obj)),...
180 'UniformOutput', false);
181 ret = types.List(cells{:});
182 else
183 ret = type_utils.nil;
184 end
185 elseif isa(obj, 'types.Nil')
186 ret = type_utils.nil;
187 else
188 if exist('OCTAVE_VERSION', 'builtin') ~= 0
189 error('Type:seq', ...
190 'seq: called on non-sequence');
191 else
192 throw(MException('Type:seq',...
193 'seq: called on non-sequence'))
194 end
195 end
196 end
197
198 function new_obj = with_meta(obj, meta)
199 new_obj = clone(obj);
200 new_obj.meta = meta;
201 end
202
203 function meta = meta(obj)
204 switch class(obj)
205 case {'types.List', 'types.Vector',
206 'types.HashMap', 'types.Function'}
207 meta = obj.meta;
208 otherwise
209 meta = type_utils.nil;
210 end
211 end
212
213 function ret = reset_BANG(atm, val)
214 atm.val = val;
215 ret = val;
216 end
217
218 function ret = swap_BANG(atm, f, varargin)
219 args = [{atm.val} varargin];
220 if isa(f, 'types.Function')
221 f = f.fn;
222 end
223 atm.val = f(args{:});
224 ret = atm.val;
225 end
226
227 function n = ns()
228 if exist('OCTAVE_VERSION', 'builtin') ~= 0
229 n = Dict();
230 else
231 n = containers.Map();
232 end
233 n('=') = @(a,b) type_utils.equal(a,b);
234 n('throw') = @(a) core.throw(a);
235 n('nil?') = @(a) isa(a, 'types.Nil');
236 n('true?') = @(a) isa(a, 'logical') && a == true;
237 n('false?') = @(a) isa(a, 'logical') && a == false;
238 n('string?') = @(a) type_utils.string_Q(a);
239 n('symbol') = @(a) types.Symbol(a);
240 n('symbol?') = @(a) isa(a, 'types.Symbol');
241 n('keyword') = @(a) type_utils.keyword(a);
242 n('keyword?') = @(a) type_utils.keyword_Q(a);
243 n('number?') = @(a) type_utils.number_Q(a);
244 n('fn?') = @(a) type_utils.fn_Q(a);
245 n('macro?') = @(a) type_utils.macro_Q(a);
246
247 n('pr-str') = @(varargin) core.pr_str(varargin{:});
248 n('str') = @(varargin) core.do_str(varargin{:});
249 n('prn') = @(varargin) core.prn(varargin{:});
250 n('println') = @(varargin) core.println(varargin{:});
251 n('read-string') = @(a) reader.read_str(a);
252 n('readline') = @(p) input(p, 's');
253 n('slurp') = @(a) fileread(a);
254
255 n('<') = @(a,b) a<b;
256 n('<=') = @(a,b) a<=b;
257 n('>') = @(a,b) a>b;
258 n('>=') = @(a,b) a>=b;
259 n('+') = @(a,b) a+b;
260 n('-') = @(a,b) a-b;
261 n('*') = @(a,b) a*b;
262 n('/') = @(a,b) floor(a/b);
263 n('time-ms') = @() core.time_ms();
264
265 n('list') = @(varargin) types.List(varargin{:});
266 n('list?') = @(a) type_utils.list_Q(a);
267 n('vector') = @(varargin) types.Vector(varargin{:});
268 n('vector?') = @(a) type_utils.vector_Q(a);
269 n('hash-map') = @(varargin) types.HashMap(varargin{:});
270 n('map?') = @(a) type_utils.hash_map_Q(a);
271 n('assoc') = @(varargin) core.assoc(varargin{:});
272 n('dissoc') = @(varargin) core.dissoc(varargin{:});
273 n('get') = @(a,b) core.get(a,b);
274 n('contains?') = @(a,b) a.data.isKey(b);
275 n('keys') = @(a) core.keys(a);
276 n('vals') = @(a) core.vals(a);
277
278 n('sequential?') = @(a) type_utils.sequential_Q(a);
279 n('cons') = @(a,b) core.cons(a,b);
280 n('concat') = @(varargin) core.concat(varargin{:});
281 n('nth') = @(a,b) core.nth(a,b);
282 n('first') = @(a) core.first(a);
283 n('rest') = @(a) core.rest(a);
284 n('empty?') = @(a) length(a) == 0;
285 % workaround Octave always giving length(a) of 1
286 n('count') = @(a) 0 + length(a);
287 n('apply') = @(varargin) core.apply(varargin{:});
288 n('map') = @(varargin) core.map(varargin{:});
289
290 n('conj') = @(varargin) core.conj(varargin{:});
291 n('seq') = @(a) core.seq(a);
292
293 n('with-meta') = @(a,b) core.with_meta(a,b);
294 n('meta') = @(a) core.meta(a);
295 n('atom') = @(a) types.Atom(a);
296 n('atom?') = @(a) isa(a, 'types.Atom');
297 n('deref') = @(a) a.val;
298 n('reset!') = @(a,b) core.reset_BANG(a,b);
299 n('swap!') = @(varargin) core.swap_BANG(varargin{:});
300 end
301 end
302 end
303