-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.arc
More file actions
192 lines (146 loc) · 3.72 KB
/
core.arc
File metadata and controls
192 lines (146 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(= none no:some
sum [reduce + _]
product [reduce * _]
range0 [range 0 dec._]
not-nil [keep id _]
ignore [do _ t]
to-list [as cons _]
to-vec [as vec _]
xlen len
ylen len:car
^ expt
values [map _ keys._]
same-count [count id (map is _a _b)]
iterable? [in type._ 'vec 'cons]
ellipsize [+ "(" _ ")"]
decimal [- _ floor._]
maximum [best > _]
minimum [best < _])
(def generate (n f) (map f range.n))
(mac next (xs) `(zap cdr ,xs))
(def remove (el xs)
(rem [iso el _] xs))
(mac or= (exp val)
`(or ,exp (= ,exp ,val)))
(mac toggle (exp)
`(= ,exp (no ,exp)))
(def prime? (n)
(and (isnt n 1) (none [multiple n _] (range 2 sqrt.n))))
(def delete-at (xs idx)
(+ (take idx xs) (drop inc.idx xs)))
(def mapcons (f pair)
(cons f:car.pair f:cdr.pair))
(mac sub (exp)
(if cons?.exp
`(cons (sub ,car.exp) (sub ,cdr.exp))
exp))
(def map-i (f xs (o n 0))
(if xs
(cons (f car.xs n)
(map-i f cdr.xs inc.n))))
(mac times (iter n . body)
`(for ,iter 0 (dec ,n) ,@body))
(mac each-idx (xs iter . body)
`(times ,iter (len ,xs) ,@body))
(def map-range0 (f xs)
(apply mapn f (mappend [list 0 dec._] xs)))
(def mapn0 (f . xs)
(map-range0 f xs))
(def factors (n)
(let under-sqrt (keep [multiple n _] range:sqrt.n)
(dedup:+ under-sqrt (rev:map [/ n _] under-sqrt))))
(def near-count (xs1 xs2)
(- (sum:map [min (count _ xs1) (count _ xs2)] dedup.xs1)
(same-count xs1 xs2)))
(def join-on (xs str)
(apply string (intersperse str xs)))
(def new-matrix ((x . dims) (o val 0))
(if x
(n-of x new-matrix.dims)
val))
(def mat dims new-matrix.dims)
(def deep-copy (xs)
(if atom.xs
xs
(map deep-copy xs)))
(def dims (mat)
(if iterable?.mat
(cons len.mat dims:mat.0)
nil))
(def in-range (val left right)
(and (>= val left) (<= val right)))
(def in-range0 (val max)
(in-range val 0 dec.max))
(def reference (obj (i . xs))
(if i
(reference obj.i xs)
obj))
(def ref (obj . xs)
(reference obj xs))
(def index? (i xs)
(and iterable?.xs (in-range0 i len.xs)))
(def referenceable? (obj (i . xs))
(no:xor i
(and i
(index? i obj)
(referenceable? obj.i xs))))
(def ref? (obj . idxs)
(referenceable? obj idxs))
(mac matrix-of ((n . dims) exp)
(if n
`(n-of ,n (matrix-of ,dims ,exp))
exp))
(mac mat-of args
`(matrix-of ,butlast.args ,last.args))
(def deep-to-vec (xs)
(if iterable?.xs
(to-vec:map deep-to-vec xs)
xs))
(mac mat-of-vec args
`(deep-to-vec:mat-of ,@args))
(mac each-idces (xs (i . idxs) . body)
(if i
`(each-idx ,xs ,i
(each-idces (ref ,xs ,i) ,idxs
,@body))
`(do ,@body)))
; given '(a b c d (e f) (g h))
; return '((a b c d) (e f) (g h))
(def wrap-tokens (xs)
(cons
(accum add
(while (and xs sym?:car.xs no:ssyntax:car.xs)
add:pop.xs))
xs))
(mac each-index (xs . body)
(let body wrap-tokens.body
`(each-idces ,xs ,@body)))
; union the keys and use that to index the tables for arguments to apply to a function
; (def map-unioned-values (f tables)
; (table-map-values f union-tables.tables))
; (def list- (list1 list2)
; (keep no:iso ))
; (mac deep-each (xs x . body)
; `(if (iterable? ,xs)
; (deep-each
; ,@body)
; (let ,x ,xs
; ,@body)))
; (mac deep-as (type obj)
; `(if (iterable? ,obj)
; (as ,type (map [deep-as ,type _] ,obj))
; ,obj))
; (times i xlen.board
; (times j ylen.board
; ...))
; (deep-each board spot
; ...)
; holy items
; (macro xs xs) - "N/A"
; (fn xs xs) - list
; [map id _] - list
; [apply list _] - id
; (fn ((l . r)) l) - car
; (fn xs car.xs) - first
; (reduce max xs) - max
; (fn xs len.xs) - argslen