# 2k: Queue Logic

## `+to` <a href="#to" id="to"></a>

Queue operations.

Container arm for `+que` operation arms. The contained arms inherit `.a`, its sample `+que`.

#### Accepts

`.a` is a tree.

#### Source

```hoon
++  to
  =|  a=(tree)  ::  (qeu)
  |@
```

#### Discussion

`+tree` is the generic tree-shape mold. Maps, sets, and `+que`s 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` <a href="#aptto" id="aptto"></a>

Check correctness.

Test whether `+que` `.a` is correctly ordered, producing a `$flag`.

#### Accepts

`.a` is a `+que`.

#### Produces

A `$flag`.

#### Source

```hoon
++  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` <a href="#balto" id="balto"></a>

Balance.

Vertically rebalances `+que` `.a`.

#### Accepts

`.a` is a `+qeu`.

#### Produces

A `+qeu`.

#### Source

```hoon
++  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))))
  a
```

#### Examples

```
> =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` <a href="#depto" id="depto"></a>

Maximum Depth.

Produces the maximum depth of leaves (`.r.a` and `.l.a`) in `+que` `.a`.

#### Accepts

`.a` is a `+que`.

#### Produces

An `$atom`.

#### Source

```hoon
++  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` <a href="#gasto" id="gasto"></a>

Push `+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

```hoon
++  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` <a href="#getto" id="getto"></a>

Head-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

```hoon
++  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` <a href="#napto" id="napto"></a>

Remove head.

Removes the head of `+que` `.a`, producing the resulting `+que`.

#### Accepts

`.a` is a `+que`.

#### Produces

A `+que`.

#### Source

```hoon
++  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` <a href="#nipto" id="nipto"></a>

Removes the root of `+que` `.a`, producing the resulting `+que`.

#### Accepts

`.a` is a `+que`.

#### Produces

A `+que`.

#### Source

```hoon
++  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` <a href="#putto" id="putto"></a>

Insert.

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

```hoon
++  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` <a href="#tapto" id="tapto"></a>

Queue to `+list`.

Produces `+que` `.a` as a `+list` from front to back.

#### Accepts

`.a` is a `+que`.

#### Produces

A `+list`.

#### Source

```hoon
++  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` <a href="#topto" id="topto"></a>

Produce head.

Produces the head of `+que` `.a` as a `+unit`.

#### Accepts

`.a` is a `+que`.

#### Produces

A unit.

#### Source

```hoon
++  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.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.urbit.org/hoon/stdlib/2k.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
