2 USING: kernel sequences assocs circular sets fry ;
4 USING: math multi-methods ;
13 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14 ! Now, we can see a new world coming into view.
15 ! A world in which there is the very real prospect of a new world order.
17 ! - George Herbert Walker Bush
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 GENERIC: at ( col key -- val )
21 GENERIC: of ( key col -- val )
23 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25 GENERIC: grab ( col key -- col val )
27 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29 GENERIC: is ( col key val -- col )
30 GENERIC: as ( col val key -- col )
32 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
34 GENERIC: is-of ( key val col -- col )
35 GENERIC: as-of ( val key col -- col )
37 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39 GENERIC: mutate-at ( col key val -- )
40 GENERIC: mutate-as ( col val key -- )
42 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
44 GENERIC: at-mutate ( key val col -- )
45 GENERIC: as-mutate ( val key col -- )
47 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
49 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
51 METHOD: at { sequence number } swap nth ;
52 METHOD: of { number sequence } nth ;
54 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
56 METHOD: grab { sequence number } dupd swap nth ;
58 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
60 METHOD: is { sequence number object } swap pick set-nth ;
61 METHOD: as { sequence object number } pick set-nth ;
63 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
65 METHOD: is-of { number object sequence } dup [ swapd set-nth ] dip ;
66 METHOD: as-of { object number sequence } dup [ set-nth ] dip ;
68 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
70 METHOD: mutate-at { sequence number object } swap rot set-nth ;
71 METHOD: mutate-as { sequence object number } rot set-nth ;
73 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
75 METHOD: at-mutate { number object sequence } swapd set-nth ;
76 METHOD: as-mutate { object number sequence } set-nth ;
78 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
80 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
82 METHOD: at { assoc object } swap assocs:at ;
83 METHOD: of { object assoc } assocs:at ;
85 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
87 METHOD: grab { assoc object } dupd swap assocs:at ;
89 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
91 METHOD: is { assoc object object } swap pick set-at ;
92 METHOD: as { assoc object object } pick set-at ;
94 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
96 METHOD: is-of { object object assoc } dup [ swapd set-at ] dip ;
97 METHOD: as-of { object object assoc } dup [ set-at ] dip ;
99 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
101 METHOD: mutate-at { assoc object object } swap rot set-at ;
102 METHOD: mutate-as { assoc object object } rot set-at ;
104 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
106 METHOD: at-mutate { object object assoc } swapd set-at ;
107 METHOD: as-mutate { object object assoc } set-at ;
109 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
111 : push ( seq obj -- seq ) over sequences:push ;
112 : push-on ( obj seq -- seq ) tuck sequences:push ;
113 : pushed ( seq obj -- ) swap sequences:push ;
114 : pushed-on ( obj seq -- ) sequences:push ;
116 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
118 : member? ( seq obj -- ? ) swap sequences:member? ;
119 : member-of? ( obj seq -- ? ) sequences:member? ;
121 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
123 : delete-at-key ( tbl key -- tbl ) over delete-at ;
124 : delete-key-of ( key tbl -- tbl ) tuck delete-at ;
126 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
128 : delete ( seq elt -- seq ) over sequences:delete ;
129 : delete-from ( elt seq -- seq ) tuck sequences:delete ;
131 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
133 : deleted ( seq elt -- ) swap sequences:delete ;
134 : deleted-from ( elt seq -- ) sequences:delete ;
136 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
138 : remove ( seq obj -- seq ) swap sequences:remove ;
139 : remove-from ( obj seq -- seq ) sequences:remove ;
141 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
143 : filter-of ( quot seq -- seq ) swap filter ;
145 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
147 : map-over ( quot seq -- seq ) swap map ;
149 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
151 : push-circular ( seq elt -- seq ) over circular:push-circular ;
153 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
155 : prefix-on ( elt seq -- seq ) swap prefix ;
156 : suffix-on ( elt seq -- seq ) swap suffix ;
158 : suffix! ( seq elt -- seq ) over sequences:push ;
159 : suffix-on! ( elt seq -- seq ) tuck sequences:push ;
160 : suffixed! ( seq elt -- ) swap sequences:push ;
161 : suffixed-on! ( elt seq -- ) sequences:push ;
163 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
165 : subseq ( seq from to -- subseq ) rot sequences:subseq ;
167 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
169 : key ( table val -- key ) swap assocs:value-at ;
171 : key-of ( val table -- key ) assocs:value-at ;
173 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
175 : index ( seq obj -- i ) swap sequences:index ;
176 : index-of ( obj seq -- i ) sequences:index ;
178 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
180 : 1st ( seq -- obj ) 0 swap nth ;
181 : 2nd ( seq -- obj ) 1 swap nth ;
182 : 3rd ( seq -- obj ) 2 swap nth ;
183 : 4th ( seq -- obj ) 3 swap nth ;
184 : 5th ( seq -- obj ) 4 swap nth ;
185 : 6th ( seq -- obj ) 5 swap nth ;
186 : 7th ( seq -- obj ) 6 swap nth ;
187 : 8th ( seq -- obj ) 7 swap nth ;
188 : 9th ( seq -- obj ) 8 swap nth ;
190 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
192 ! A note about the 'mutate' qualifier. Other words also technically mutate
193 ! their primary object. However, the 'mutate' qualifier is supposed to
194 ! indicate that this is the main objective of the word, as a side effect.
196 : adjoin ( seq elt -- seq ) over sets:adjoin ;
197 : adjoin-on ( elt seq -- seq ) tuck sets:adjoin ;
198 : adjoined ( set elt -- ) swap sets:adjoin ;
199 : adjoined-on ( elt set -- ) sets:adjoin ;
201 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
203 : start ( seq subseq -- i ) swap sequences:start ;
205 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
207 : pluck ( seq i -- seq ) cut-slice rest-slice append ;
208 : pluck-from ( i seq -- seq ) swap pluck ;
209 : pluck! ( seq i -- seq ) over delete-nth ;
210 : pluck-from! ( i seq -- seq ) tuck delete-nth ;
211 : plucked! ( seq i -- ) swap delete-nth ;
212 : plucked-from! ( i seq -- ) delete-nth ;
214 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
216 : snip ( seq a b -- seq ) [ over ] dip [ head ] [ tail ] 2bi* append ;
217 : snip-this ( a b seq -- seq ) -rot snip ;
218 : snip! ( seq a b -- seq ) pick delete-slice ;
219 : snip-this! ( a b seq -- seq ) -rot pick delete-slice ;
220 : snipped! ( seq a b -- ) rot delete-slice ;
221 : snipped-from! ( a b seq -- ) delete-slice ;
223 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
225 : invert-index ( seq i -- seq i ) [ dup length 1 - ] dip - ;
227 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
229 : append! ( a b -- ab ) over sequences:push-all ;
230 : append-to! ( b a -- ab ) swap over sequences:push-all ;
231 : appended! ( a b -- ) swap sequences:push-all ;
232 : appended-to! ( b a -- ) sequences:push-all ;
234 : prepend! ( a b -- ba ) over append 0 pick copy ;
235 : prepended! ( a b -- ) over append 0 rot copy ;
237 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
239 : insert ( seq i obj -- seq ) [ cut ] dip prefix append ;
241 : splice ( seq i seq -- seq ) [ cut ] dip prepend append ;
243 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
245 : purge ( seq quot -- seq ) [ not ] compose filter ;
247 : purge! ( seq quot -- seq )
248 dupd '[ swap @ [ pluck! ] [ drop ] if ] each-index ;