swift: Fix (rest nil)
[jackhill/mal.git] / groovy / core.groovy
CommitLineData
a9cd6543
JM
1import types
2import types.MalException
3import types.MalSymbol
4import reader
5import printer
6
7class core {
8 def static do_pr_str(args) {
9 return printer._pr_list(args, " ", true)
10 }
11 def static do_str(args) {
12 return printer._pr_list(args, "", false)
13 }
14 def static do_prn(args) {
15 println(printer._pr_list(args, " ", true))
16 }
17 def static do_println(args) {
18 println(printer._pr_list(args, " ", false))
19 }
20
21 def static do_concat(args) {
22 args.inject([], { a, b -> a + (b as List) })
23 }
24 def static do_nth(args) {
25 if (args[0].size() <= args[1]) {
26 throw new MalException("nth: index out of range")
27 }
28 args[0][args[1]]
29 }
4ed89670
JM
30 def static do_conj(args) {
31 if (types.list_Q(args[0])) {
32 args.drop(1).inject(args[0], { a, b -> [b] + a })
33 } else {
34 types.vector(args.drop(1).inject(args[0], { a, b -> a + [b] }))
35 }
36 }
a9cd6543
JM
37 def static do_apply(args) {
38 def start_args = args.drop(1).take(args.size()-2) as List
39 args[0](start_args + (args.last() as List))
40 }
41
42 def static do_swap_BANG(args) {
43 def (atm,f) = [args[0], args[1]]
44 atm.value = f([atm.value] + (args.drop(2) as List))
45 }
46
47 static ns = [
48 "=": { a -> a[0]==a[1]},
49 "throw": { a -> throw new MalException(a[0]) },
50
51 "nil?": { a -> a[0] == null },
52 "true?": { a -> a[0] == true },
53 "false?": { a -> a[0] == false },
54 "symbol": { a -> new MalSymbol(a[0]) },
55 "symbol?": { a -> a[0] instanceof MalSymbol },
56 "keyword": { a -> types.keyword(a[0]) },
57 "keyword?": { a -> types.keyword_Q(a[0]) },
58
59 "pr-str": core.&do_pr_str,
60 "str": core.&do_str,
61 "prn": core.&do_prn,
62 "println": core.&do_println,
63 "read-string": reader.&read_str,
64 "readline": { a -> System.console().readLine(a[0]) },
65 "slurp": { a -> new File(a[0]).text },
66
67 "<": { a -> a[0]<a[1]},
68 "<=": { a -> a[0]<=a[1]},
69 ">": { a -> a[0]>a[1]},
70 ">=": { a -> a[0]>=a[1]},
71 "+": { a -> a[0]+a[1]},
72 "-": { a -> a[0]-a[1]},
73 "*": { a -> a[0]*a[1]},
74 "/": { a -> a[0]/a[1]}, // /
75 "time-ms": { a -> System.currentTimeMillis() },
76
77 "list": { a -> a},
78 "list?": { a -> types.list_Q(a[0]) },
79 "vector": { a -> types.vector(a) },
80 "vector?": { a -> types.vector_Q(a[0]) },
81 "hash-map": { a -> types.hash_map(a) },
82 "map?": { a -> types.hash_map_Q(a[0]) },
83 "assoc": { a -> types.assoc_BANG(types.copy(a[0]), a.drop(1)) },
84 "dissoc": { a -> types.dissoc_BANG(types.copy(a[0]), a.drop(1)) },
85 "get": { a -> a[0] == null ? null : a[0][a[1]] },
86 "contains?": { a -> a[0].containsKey(a[1]) },
87 "keys": { a -> a[0].keySet() as List },
88 "vals": { a -> a[0].values() as List },
89
90 "sequential?": { a -> types.&sequential_Q(a[0]) },
91 "cons": { a -> [a[0]] + (a[1] as List) },
92 "concat": core.&do_concat,
93 "nth": core.&do_nth,
94 "first": { a -> a[0].size() == 0 ? null : a[0][0] },
95 "rest": { a -> a[0].drop(1) },
96 "empty?": { a -> a[0] == null || a[0].size() == 0 },
97 "count": { a -> a[0] == null ? 0 : a[0].size() },
98 "apply": core.&do_apply,
99 "map": { a -> a[1].collect { x -> a[0].call([x]) } },
100
4ed89670 101 "conj": core.&do_conj,
a9cd6543
JM
102
103 "meta": { a -> a[0].hasProperty("meta") ? a[0].getProperties().meta : null },
104 "with-meta": { a -> def b = types.copy(a[0]); b.getMetaClass().meta = a[1]; b },
105 "atom": { a -> new types.MalAtom(a[0]) },
106 "atom?": { a -> a[0] instanceof types.MalAtom },
107 "deref": { a -> a[0].value },
108 "reset!": { a -> a[0].value = a[1] },
109 "swap!": core.&do_swap_BANG
110 ]
111}
112