/
builtins.lisp
205 lines (168 loc) · 4.67 KB
/
builtins.lisp
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
193
194
195
196
197
198
199
200
201
(in-package #:com.clearly-useful.generic-collection-interface)
(extend-type null
collection
(empty (o) nil)
(empty-p (o) t)
seqable
(seq (o) nil)
seq
(head (o) nil)
(tail (o) nil)
countable
(counted-p (o) t)
(count-elements (o) 0)
indexable
(element-at (o index) (%seq-nth-or-error o index))
associative
(all-keys (o) nil)
(all-values (o) nil)
(contains-key-p (o k) (declare (ignorable o k)) nil)
(value-for-key (o k) (values nil nil))
reduceable
(coll-reduce (self fn seed) (declare (ignorable self)
(ignorable fn)) seed))
(extend-type cons
collection
(empty (o) nil)
(empty-p (o) nil)
seqable
(seq (o) o)
seq
(head (o) (car o))
(tail (o) (cdr o))
countable
(counted-p (o) (declare (ignorable o)) nil)
(count-elements (o) (%count-seq o))
associative
(all-keys (o) (make-%range :low 0 :high (count-elements o)))
(all-values (o) o)
(contains-key-p (o key)
(and (integerp key))
(<= -1 key (count-elements o)))
(value-for-key (o index)
(%seq-nth-or-nil-with-values o index))
reduceable
(coll-reduce (self fn seed)
(let ((r seed))
(doseq (v self r)
(setf r (funcall fn r v))))))
(extend-type vector
collection
(empty (it) (make-array 0
:element-type (array-element-type it)))
(empty-p (it) (zerop (length it)))
seqable
(seq (it) (when (plusp (length it))
it))
seq
(head (it) (elt it 0))
(tail (it) (let ((len (1- (length it))))
(unless (zerop len)
(make-array len
:displaced-to it
:displaced-index-offset 1
:element-type
(array-element-type it)))))
countable
(counted-p (it) t)
(count-elements (it) (length it))
indexable
(element-at (it n) (elt it n))
associative
(all-keys (it) (make-%range :low 0 :high (length it)))
(all-values (it) it)
(contains-key-p (it key)
(and (integerp key)
(<= -1 key (length it))))
(value-for-key (it key)
(if (contains-key-p it key)
(values (elt it key) t)
(values nil nil)))
reduceable
(coll-reduce (self fn seed) (reduce fn self :initial-value seed))
foldable
#+com.clearly-useful.threading-supported
(coll-fold (self n combinef reducef)
(fold-vector self n combinef reducef))
#-com.clearly-useful.threading-supported
(coll-fold (self n combinef reducef)
(declare (ignore n))
(reduce reducef self :initial-value (funcall combinef)))
)
(extend-type array
;;should this retain the dimensionality of array
;;or return an empty vector?
;;keeping the rank around for now, seems
;;better to avoid information loss...
collection
(empty (it) (make-array (make-list (array-rank it)
:initial-element 0)
:element-type (array-element-type it)))
(empty-p (it) (zerop (array-total-size it)))
seqable
(seq (it) (when (plusp (array-total-size it))
(make-array (array-total-size it)
:displaced-to it
:displaced-index-offset 0
:element-type
(array-element-type it))))
countable
(counted-p (it) t)
(count-elements (it) (array-total-size it))
indexable
(element-at (it n) (row-major-aref it n))
associative
(all-keys (it) (make-%range :low 0 :high (array-total-size it)))
(all-values (it) (make-array (array-total-size it)
:displaced-to it
:displaced-index-offset 0
:element-type
(array-element-type it)))
(contains-key-p (it key)
(and (integerp key)
(<= -1 key (array-total-size it))))
(value-for-key (it key)
(if (contains-key-p it key)
(values (row-major-aref it key) t)
(values nil nil)))
reduceable
(coll-reduce (self fn seed)
(reduce fn (make-array (array-total-size self)
:displaced-to self
:displaced-index-offset 0
:element-type (array-element-type self))
:initial-value seed)))
(defmethod all-keys-and-values ((a hash-table))
(loop for k being the hash-keys of a
using (hash-value v)
collect (list k v)))
(extend-type hash-table
collection
(empty (it) (%empty-hash it))
(empty-p (it) (zerop (hash-table-count it)))
seqable
(seq (it) (when (plusp (hash-table-count it))
(all-keys-and-values it)))
countable
(counted-p (it) t)
(count-elements (it) (hash-table-count it))
associative
(all-keys (it)
(loop for k being
the hash-keys of it
collect k))
(all-values (it)
(loop for v being
the hash-values of it
collect v))
(contains-key-p (o k)
(nth-value 1 (gethash k o)))
(value-for-key (o k)
(gethash k o))
reduceable
(coll-reduce (self fn seed)
(let ((r seed))
(maphash (lambda (k v)
(setf r (funcall fn r (list k v))))
self)
r)))