2k: Queue Logic
+to
+toQueue operations.
Container arm for +que operation arms. The contained arms inherit .a, its sample +que.
Accepts
.a is a tree.
Source
++ to
=| a=(tree) :: (qeu)
|@Discussion
+tree is the generic tree-shape mold. Maps, sets, and +ques all share that shape, but they put their elements in different places and have different interfaces. +to is the interface core for +qeu; it's not generic across all the different containers that use +tree.
+apt:to
+apt:toCheck correctness.
Test whether +que .a is correctly ordered, producing a $flag.
Accepts
.a is a +que.
Produces
A $flag.
Source
++ apt
|- ^- ?
?~ a &
?& ?~(l.a & ?&((mor n.a n.l.a) $(a l.a)))
?~(r.a & ?&((mor n.a n.r.a) $(a r.a)))
==Examples
> =bad `(qeu @ud)`[1 ~ 2 ~ 3 ~ ~]
> ~(apt to bad)
%.n> =good `(qeu @ud)`(~(gas to `(qeu @ud)`~) ~[1 2 3])
> ~(apt to good)
%.y+bal:to
+bal:toBalance.
Vertically rebalances +que .a.
Accepts
.a is a +qeu.
Produces
A +qeu.
Source
++ bal
|- ^+ a
?~ a ~
?. |(?=(~ l.a) (mor n.a n.l.a))
$(a l.a(r $(a a(l r.l.a))))
?. |(?=(~ r.a) (mor n.a n.r.a))
$(a r.a(l $(a a(r l.r.a))))
aExamples
> =a `(qeu @ud)`[1 ~ 2 ~ 3 ~ ~]
> a
{1 2 3}
> `*`a
[1 0 2 0 3 0 0]
> ~(apt to a)
%.n
> =z `(qeu @ud)`~(bal to a)
> z
{1 2 3}
> `*`z
[2 [1 0 0] 3 0 0]
> ~(apt to z)
%.y+dep:to
+dep:toMaximum Depth.
Produces the maximum depth of leaves (.r.a and .l.a) in +que .a.
Accepts
.a is a +que.
Produces
An $atom.
Source
++ dep
|- ^- @
?~ a 0
+((max $(a l.a) $(a r.a)))Examples
> =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 7 ~])
> ~(dep to a)
4
> =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 ~])
> ~(dep to a)
3
> =a (~(gas to `(qeu @)`~) `(list @)`[1 2 ~])
> ~(dep to a)
2
> ~(dep to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~])
7
> ~(dep to ~(bal to `(qeu tape)`["a" ~ "b" ~ "c" ~ "d" ~ "e" ~ "f" ~ "g" ~ ~]))
4+gas:to
+gas:toPush +list.
Push all elements of +list .b into the +que .a.
Accepts
.a is a +que, and is the sample of +to.
.b is a +list.
Produces
A +que.
Source
++ gas
|= b=(list _?>(?=(^ a) n.a))
|- ^+ a
?~(b a $(b t.b, a (put i.b)))Examples
> `(qeu)`(~(gas to `(qeu @)`~) [1 2 3 ~])
{3 2 1}> =a (~(gas to `(qeu @)`~) [1 2 3 ~])
> =b [4 5 6 ~]
> `(qeu)`(~(gas to a) b)
{6 5 4 3 2 1}+get:to
+get:toHead-tail pair.
Produces the head and tail +que of .a.
Accepts
.a is a +que.
Produces
A cell of the last element in .a along with the rest of +que .a.
Source
++ get
|- ^+ ?>(?=(^ a) [p=n.a q=*(tree _n.a)])
?~ a
!!
?~ r.a
[n.a l.a]
=+ b=$(a r.a)
:- p.b
?: |(?=(~ q.b) (mor n.a n.q.b))
a(r q.b)
a(n n.q.b, l a(r l.q.b), r r.q.b)Examples
> =s (~(gas to *(tree @)) `(list @)`~[1 2 3])
> ~(get to s)
[p=1 q={3 2}]
> ~(get to ~)
dojo: hoon expression failed+nap:to
+nap:toRemove head.
Removes the head of +que .a, producing the resulting +que.
Accepts
.a is a +que.
Produces
A +que.
Source
++ nap
?> ?=(^ a)
?: =(~ l.a) r.a
=+ b=get(a l.a)
bal(n.a p.b, l.a q.b)Examples
> =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
> -.a
n=6
> =b ~(nap to a)
> -.b
n=2
> a
[n=6 l={} r={5 4 3 2 1}]
> b
[n=2 l=[n=4 l={5} r={3}] r=[n=1 l={} r={}]]
> `(qeu)`b
{5 4 3 2 1}
> `(qeu)`a
{6 5 4 3 2 1}+nip:to
+nip:toRemoves the root of +que .a, producing the resulting +que.
Accepts
.a is a +que.
Produces
A +que.
Source
++ nip
|- ^+ a
?~ a ~
?~ l.a r.a
?~ r.a l.a
?: (mor n.l.a n.r.a)
l.a(r $(l.a r.l.a))
r.a(l $(r.a l.r.a))Examples
> =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
> a
[n=6 l={} r={5 4 3 2 1}]
> ~(nip to a)
[n=2 l=[n=4 l={5} r={3}] r=[n=1 l={} r={}]]+put:to
+put:toInsert.
Accept $noun .b and adds to +que .a as the head, producing the resulting +que.
Accepts
.a is a +que, and is the sample of +to.
.b is a $noun.
Produces
A +que.
Source
++ put
|* b=*
|- ^+ a
?~ a
[b ~ ~]
bal(l.a $(a l.a))Examples
> =a (~(gas to `(qeu @)`~) `(list @)`[3 1 2 4 5 6 ~])
> `(qeu)`(~(put to a) 7)
{7 6 5 4 2 1 3}+tap:to
+tap:toQueue to +list.
Produces +que .a as a +list from front to back.
Accepts
.a is a +que.
Produces
A +list.
Source
++ tap
=+ b=`(list _?>(?=(^ a) n.a))`~
|- ^+ b
=+ 0 :: hack for jet match
?~ a
b
$(a r.a, b [n.a $(a l.a)])Examples
> =a (~(gas to `(qeu @)`~) [3 1 2 4 5 6 ~])
> `*`a
[6 0 2 [4 [5 0 0] 0] 1 0 3 0 0]
> ~(tap to a)
~[3 1 2 4 5 6]+top:to
+top:toProduce head.
Produces the head of +que .a as a +unit.
Accepts
.a is a +que.
Produces
A unit.
Source
++ top
|- ^- (unit _?>(?=(^ a) n.a))
?~ a ~
?~(r.a [~ n.a] $(a r.a))Examples
> =a (~(gas to `(qeu @)`~) `(list @)`[1 2 3 4 5 6 ~])
> ~(top to a)
[~ u=1]Discussion
An empty +que has no head.
Last updated