on bcfe82c help sign in
398 lines 5.8 kB history raw
1+++
2title = "2k: Queue Logic"
3weight = 14
4template = "doc.html"
5+++
6### `++to`
9Queue operations
11Container arm for queue operation arms. The contained arms inherit `a`, its
12sample queue.
14#### Accepts
16`a` is a tree.
18#### Source
20```
21 ++ to
22 |_ a=(tree)
23```
25#### Discussion
27`tree` is the generic tree-shape mold. Maps, sets, and queues all share that
28shape, but they put their elements in different places and have different
29interfaces. `to` is the interface core for `qeu`; it's not generic across all
30the different containers that use `tree`.
32---
33### `+-bal:to`
35Balance
37Vertically rebalances queue `a`.
39#### Accepts
41`a` is a queue.
43#### Produces
45A queue.
47#### Source
49```
50 +- bal
51 |- ^+ a
52 ?~ a ~
53 ?. |(?=($~ l.a) (vor n.a n.l.a))
54 $(a [n.l.a l.l.a $(a [n.a r.l.a r.a])])
55 ?. |(?=($~ r.a) (vor n.a n.r.a))
56 $(a [n.r.a $(a [n.a l.a l.r.a]) r.r.a])
57 a
58```
60#### Examples
62```
63 > `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]
64 {"a" "b" "c" "d" "e" "f" "g"}
66 > `*`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]
67 [[97 0] 0 [98 0] 0 [99 0] 0 [100 0] 0 [101 0] 0 [102 0] 0 [103 0] 0 0]
69 > ~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
70 [n=[i='d' t=""] l={"a" "b" "c"} r={"e" "f" "g"}]
72 > `*`~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
73 [[100 0] [[99 0] [[98 0] [[97 0] 0 0] 0] 0] [101 0] 0 [102 0] 0 [103 0] 0 0]
74```
76---
77### `+-dep:to`
79Maximum Depth
81Produces the maximum depth of leaves (`r.a` and `l.a`) in queue `a`.
83#### Accepts
85`a` is a queue.
87#### Produces
89An atom.
91#### Source
93```
94 +- dep
95 |- ^- @
96 ?~ a 0
97 +((max $(a l.a) $(a r.a)))
98```
100#### Examples
102```
103 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 7 ~])
104 > ~(dep to a)
105 4
107 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 ~])
108 > ~(dep to a)
109 3
111 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 ~])
112 > ~(dep to a)
113 2
115 > ~(dep to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
116 7
118 > ~(dep to ~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]))
119 4
120```
122---
123### `+-gas:to`
125Push list
127Push all elements of list `b` into the queue `a`.
129#### Accepts
131`a` is a queue.
133`b` is a list.
135#### Produces
137A queue.
139#### Source
141```
142 +- gas
143 |= b=(list _?>(?=(^ a) n.a))
144 |- ^+ a
145 ?~(b a $(b t.b, a (put i.b)))
146```
149#### Examples
151```
152 > `(qeu)`(~(gas to `(qeu @)`~) [1 2 3 ~])
153 {3 2 1}
155 > =a (~(gas to `(qeu @)`~) [1 2 3 ~])
156 > =b [4 5 6 ~]
157 > `(qeu)`(~(gas to a) b)
158 {6 5 4 3 2 1}
159```
160---
161### `+-get:to`
163Head-tail pair
165Produces the head and tail queue of `a`.
167#### Accepts
169`a` is a queue.
171#### Produces
173A cell of the last element in `a` along with the rest of queue `a`.
175#### Source
177```
178 +- get
179 |- ^+ ?>(?=(^ a) [p=n.a q=*(tree _n.a)])
180 ?~ a
181 !!
182 ?~ r.a
183 [n.a l.a]
184 =+ b=$(a r.a)
185 :- p.b
186 ?: |(?=($~ q.b) (vor n.a n.q.b))
187 [n.a l.a q.b]
188 [n.q.b [n.a l.a l.q.b] r.q.b]
189```
191#### Examples
193```
194 > =s (~(gas to *(tree @)) `(list @)`~[1 2 3])
195 > ~(get to s)
196 [n=2 l={3} r={1}]
198 > ~(get to ~)
199 ! exit
200```
202---
203### `+-nap:to`
205Remove head
207Removes the head of queue `a`, producing the resulting queue.
209#### Accepts
211`a` is a queue.
213#### Produces
215A queue.
217#### Source
219```
220 +- nap
221 ?> ?=(^ a)
222 ?~ l.a r.a
223 =+ b=get(a l.a)
224 bal(a ^+(a [p.b q.b r.a]))
225```
227#### Examples
229```
230 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
231 > -.a
232 n=6
234 > =b ~(nap to a)
235 > -.b
236 n=2
238 > a
239 [n=6 l={} r={5 4 3 2 1}]
241 > b
242 [n=2 l=[n=4 l={5} r={3}] r=[n=1 l={} r={}]]
244 > `(qeu)`b
245 {5 4 3 2 1}
247 > `(qeu)`a
248 {6 5 4 3 2 1}
249```
251---
252### `+-nip:to`
254Removes the root of queue `a`, producing the resulting queue.
256#### Accepts
258`a` is a queue.
260#### Produces
262A queue.
264#### Source
266```
267 +- nip
268 |- ^+ a
269 ?~ a ~
270 ?~ l.a r.a
271 ?~ r.a l.a
272 ?: (vor n.l.a n.r.a)
273 [n.l.a l.l.a $(l.a r.l.a)]
274 [n.r.a $(r.a l.r.a) r.r.a]
275```
277#### Examples
279```
280 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
281 > a
282 [n=6 l={} r={5 4 3 2 1}]
284 > ~(nip to a)
285 [n=2 l=[n=4 l={5} r={3}] r=[n=1 l={} r={}]]
286```
288---
289### `+-put:to`
291Insert
293Accept any noun `b` and adds to queue `a` as the head, producing the
294resulting queue.
296#### Accepts
298`a` is a queue.
300`b` is any noun.
302#### Produces
304A queue.
306#### Source
308```
309 +- put
310 |* b=*
311 |- ^+ a
312 ?~ a
313 [b ~ ~]
314 bal(a a(l $(a l.a)))
315```
317#### Examples
319```
320 > =a (~(gas to `(qeu @)`~) `(list @)`[3 1 2 4 5 6 ~])
321 > `(qeu)`(~(put to a) 7)
322 {7 6 5 4 2 1 3}
323```
325---
326### `+-tap:to`
328Queue to list
330Produces queue `a` as a list from front to back.
332#### Accepts
334`a` is a queue.
336#### Produces
338A list.
340#### Source
342```
343 +- tap
344 =| b=(list _?>(?=(^ a) n.a))
345 |- ^+ b
346 =+ 0 :: hack for jet match
347 ?~ a
348 b
349 $(a r.a, b [n.a $(a l.a)])
350```
352#### Examples
354```
355 > =a (~(gas to `(qeu @)`~) [3 1 2 4 5 6 ~])
356 > `*`a
357 [6 0 2 [4 [5 0 0] 0] 1 0 3 0 0]
358 > ~(tap to a)
359 ~[3 1 2 4 5 6]
360```
362---
363### `+-top:to`
365Produce head
367Produces the head of queue `a` as a `unit`.
369#### Accepts
371`a` is a queue.
373#### Produces
375A unit.
377#### Source
379```
380 +- top
381 |- ^- (unit _?>(?=(^ a) n.a))
382 ?~ a ~
383 ?~(r.a [~ n.a] $(a r.a))
384```
386#### Examples
388```
389 > =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
390 > ~(top to a)
391 [~ u=1]
392```
394#### Discussion
396An empty queue has no head.
398---