on c19bc7a help sign in
403 lines 5.9 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'14'
3 title/'2k: Queue Logic'
4 ==
6;>
9# 2k: queue logic
11### `++to`
14Queue operations
16Container arm for queue operation arms. The contained arms inherit `a`, its
17sample queue.
19#### Accepts
21`a` is a tree.
23#### Source
25```
26 ++ to
27 |_ a=(tree)
28```
30#### Discussion
32`tree` is the generic tree-shape mold. Maps, sets, and queues all share that
33shape, but they put their elements in different places and have different
34interfaces. `to` is the interface core for `qeu`; it's not generic across all
35the different containers that use `tree`.
37---
38### `+-bal:to`
40Balance
42Vertically rebalances queue `a`.
44#### Accepts
46`a` is a queue.
48#### Produces
50A queue.
52#### Source
54```
55 +- bal
56 |- ^+ a
57 ?~ a ~
58 ?. |(?=($~ l.a) (vor n.a n.l.a))
59 $(a [n.l.a l.l.a $(a [n.a r.l.a r.a])])
60 ?. |(?=($~ r.a) (vor n.a n.r.a))
61 $(a [n.r.a $(a [n.a l.a l.r.a]) r.r.a])
62 a
63```
65#### Examples
67```
68 > `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]
69 {"a" "b" "c" "d" "e" "f" "g"}
71 > `*`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]
72 [[97 0] 0 [98 0] 0 [99 0] 0 [100 0] 0 [101 0] 0 [102 0] 0 [103 0] 0 0]
74 > ~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
75 [n=[i='d' t=""] l={"a" "b" "c"} r={"e" "f" "g"}]
77 > `*`~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
78 [[100 0] [[99 0] [[98 0] [[97 0] 0 0] 0] 0] [101 0] 0 [102 0] 0 [103 0] 0 0]
79```
81---
82### `+-dep:to`
84Maximum Depth
86Produces the maximum depth of leaves (`r.a` and `l.a`) in queue `a`.
88#### Accepts
90`a` is a queue.
92#### Produces
94An atom.
96#### Source
98```
99 +- dep
100 |- ^- @
101 ?~ a 0
102 +((max $(a l.a) $(a r.a)))
103```
105#### Examples
107```
108 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 7 ~])
109 > ~(dep to a)
110 4
112 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 ~])
113 > ~(dep to a)
114 3
116 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 ~])
117 > ~(dep to a)
118 2
120 > ~(dep to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
121 7
123 > ~(dep to ~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]))
124 4
125```
127---
128### `+-gas:to`
130Push list
132Push all elements of list `b` into the queue `a`.
134#### Accepts
136`a` is a queue.
138`b` is a list.
140#### Produces
142A queue.
144#### Source
146```
147 +- gas
148 |= b=(list _?>(?=(^ a) n.a))
149 |- ^+ a
150 ?~(b a $(b t.b, a (put i.b)))
151```
154#### Examples
156```
157 > `(qeu)`(~(gas to `(qeu @)`~) [1 2 3 ~])
158 {3 2 1}
160 > =a (~(gas to `(qeu @)`~) [1 2 3 ~])
161 > =b [4 5 6 ~]
162 > `(qeu)`(~(gas to a) b)
163 {6 5 4 3 2 1}
164```
165---
166### `+-get:to`
168Head-tail pair
170Produces the head and tail queue of `a`.
172#### Accepts
174`a` is a queue.
176#### Produces
178A cell of the last element in `a` along with the rest of queue `a`.
180#### Source
182```
183 +- get
184 |- ^+ ?>(?=(^ a) [p=n.a q=*(tree _n.a)])
185 ?~ a
186 !!
187 ?~ r.a
188 [n.a l.a]
189 =+ b=$(a r.a)
190 :- p.b
191 ?: |(?=($~ q.b) (vor n.a n.q.b))
192 [n.a l.a q.b]
193 [n.q.b [n.a l.a l.q.b] r.q.b]
194```
196#### Examples
198```
199 > =s (~(gas to *(tree @)) `(list @)`~[1 2 3])
200 > ~(get to s)
201 [n=2 l={3} r={1}]
203 > ~(get to ~)
204 ! exit
205```
207---
208### `+-nap:to`
210Remove head
212Removes the head of queue `a`, producing the resulting queue.
214#### Accepts
216`a` is a queue.
218#### Produces
220A queue.
222#### Source
224```
225 +- nap
226 ?> ?=(^ a)
227 ?~ l.a r.a
228 =+ b=get(a l.a)
229 bal(a ^+(a [p.b q.b r.a]))
230```
232#### Examples
234```
235 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
236 > -.a
237 n=6
239 > =b ~(nap to a)
240 > -.b
241 n=2
243 > a
244 [n=6 l={} r={5 4 3 2 1}]
246 > b
247 [n=2 l=[n=4 l={5} r={3}] r=[n=1 l={} r={}]]
249 > `(qeu)`b
250 {5 4 3 2 1}
252 > `(qeu)`a
253 {6 5 4 3 2 1}
254```
256---
257### `+-nip:to`
259Removes the root of queue `a`, producing the resulting queue.
261#### Accepts
263`a` is a queue.
265#### Produces
267A queue.
269#### Source
271```
272 +- nip
273 |- ^+ a
274 ?~ a ~
275 ?~ l.a r.a
276 ?~ r.a l.a
277 ?: (vor n.l.a n.r.a)
278 [n.l.a l.l.a $(l.a r.l.a)]
279 [n.r.a $(r.a l.r.a) r.r.a]
280```
282#### Examples
284```
285 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
286 > a
287 [n=6 l={} r={5 4 3 2 1}]
289 > ~(nip to a)
290 [n=2 l=[n=4 l={5} r={3}] r=[n=1 l={} r={}]]
291```
293---
294### `+-put:to`
296Insert
298Accept any noun `b` and adds to queue `a` as the head, producing the
299resulting queue.
301#### Accepts
303`a` is a queue.
305`b` is any noun.
307#### Produces
309A queue.
311#### Source
313```
314 +- put
315 |* b=*
316 |- ^+ a
317 ?~ a
318 [b ~ ~]
319 bal(a a(l $(a l.a)))
320```
322#### Examples
324```
325 > =a (~(gas to `(qeu @)`~) `(list @)`[3 1 2 4 5 6 ~])
326 > `(qeu)`(~(put to a) 7)
327 {7 6 5 4 2 1 3}
328```
330---
331### `+-tap:to`
333Queue to list
335Produces queue `a` as a list from front to back.
337#### Accepts
339`a` is a queue.
341#### Produces
343A list.
345#### Source
347```
348 +- tap
349 =| b=(list _?>(?=(^ a) n.a))
350 |- ^+ b
351 =+ 0 :: hack for jet match
352 ?~ a
353 b
354 $(a r.a, b [n.a $(a l.a)])
355```
357#### Examples
359```
360 > =a (~(gas to `(qeu @)`~) [3 1 2 4 5 6 ~])
361 > `*`a
362 [6 0 2 [4 [5 0 0] 0] 1 0 3 0 0]
363 > ~(tap to a)
364 ~[3 1 2 4 5 6]
365```
367---
368### `+-top:to`
370Produce head
372Produces the head of queue `a` as a `unit`.
374#### Accepts
376`a` is a queue.
378#### Produces
380A unit.
382#### Source
384```
385 +- top
386 |- ^- (unit _?>(?=(^ a) n.a))
387 ?~ a ~
388 ?~(r.a [~ n.a] $(a r.a))
389```
391#### Examples
393```
394 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
395 > ~(top to a)
396 [~ u=1]
397```
399#### Discussion
401An empty queue has no head.
403---