> For the complete documentation index, see [llms.txt](https://docs.urbit.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.urbit.org/hoon/stdlib/2n.md).

# 2n: Functional Hacks

Functional programming utilities including composition, currying, and common higher-order functions.

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

Pair after.

`+aftr` first takes gate `.a`, producing a wet gate. The new wet gate then takes `.b`, producing the inverted pair of `[b a]`. This is the inverted version of [`+fore`](#fore).

#### Accepts

`.a` is a gate, and the sample of `+aftr`.

`.b` is a gate, and the sample of `(aftr a)`.

#### Produces

`(pair b a)`.

#### Source

```hoon
++  aftr  |*(a=$-(* *) |*(b=$-(* *) (pair b a)))
```

#### Examples

```
> =a (aftr @ud)

> `(a @t)`['foo' 42]
[p='foo' q=42]
```

***

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

Compose forward.

Call gate `.a`, then call gate `.b` with its product.

This is a wet gate that takes two gates and produces a new gate.

This is the inverse of [`+corl`](#corl).

#### Accepts

`.a` is a gate.

`.b` is a gate.

#### Source

```hoon
++  cork  |*([a=$-(* *) b=$-(* *)] (corl b a))
```

#### Examples

```
> ((cork dec |=(a=@ [a a])) 20)
[19 19]

> ((cork dec some) 20)
[~ u=19]
```

***

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

Compose backward.

Call gate `.b`, then call gate `.a` with its product.

This is a wet gate that takes two gates and produces a new gate.

This is the inverse of [`+cork`](#cork).

#### Accepts

`.a` is a gate.

`.b` is a gate.

#### Source

```hoon
++  corl
  |*  [a=$-(* *) b=$-(* *)]
  =<  +:|.((a (b)))
  |*  c=_,.+<.b
  (a (b c))
```

#### Examples

```
> ((corl |=(a=@ [a a]) dec) 20)
[19 19]
```

```
> ((corl some dec) 20)
[~ u=19]
```

***

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

Right curry.

Right-curry a gate, binding the tail of its sample

#### Accepts

`.a` is a gate.

`.c` is a `$noun`.

#### Produces

A gate.

#### Source

```hoon
++  curr
  |*  [a=$-(^ *) c=*]
  |*  b=_,.+<-.a
  (a b c)
```

#### Examples

```
> =tep (curr scan sym)

> `@t`(tep "asd")
'asd'

> `@t`(tep "lek-om")
'lek-om'
```

***

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

Curry left.

Curry a gate, binding the head of its sample.

#### Accepts

`.a` is a gate.

`.b` is a `$noun`.

#### Produces

A gate.

#### Source

```hoon
++  cury
  |*  [a=$-(^ *) b=*]
  |*  c=_,.+<+.a
  (a b c)
```

#### Examples

```
> =mol (cury add 2)

> (mol 4)
6

> (mol 7)
9
```

***

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

Pair before.

`+fore` first takes gate `.a`, producing a wet gate. The new wet gate then takes `.b`, producing the pair of `[a b]`.

#### Accepts

`.a` is a gate, and is the sample of `+fore`.

`.b` is a gate, and is the sample of `(fore a)`.

#### Produces

`(pair a b)`.

#### Source

```hoon
++  fore  |*(a=$-(* *) |*(b=$-(* *) (pair a b)))
```

#### Examples

```
> =a (fore @ud)

> `(a @t)`[42 'foo']
[p=42 q='foo']
```

***

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

Get head.

Produces the head of a cell.

#### Accepts

A cell.

#### Produces

A `$noun`.

#### Source

```hoon
++  head  |*(^ ,:+<-)
```

#### Examples

```
> (head [1 2])
1
```

```
> (head [[1 1] 2])
[1 1]
```

```
> (head "hello")
'h'
```

***

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

Identity.

Produces the same value that it was given.

#### Accepts

A `$noun`.

#### Produces

A `$noun`.

#### Source

```hoon
++  same  |*(* +<)
```

#### Examples

```
> (same [1 2])
[1 2]
```

```
> (same [[1 1] 2])
[[1 1] 2]
```

```
> (same "hello")
"hello"
```

***

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

Successor.

Increment an `$atom`.

#### Accepts

An `$atom`.

#### Produces

An `$atom`.

#### Source

```hoon
++  succ  |=(@ +(+<))
```

#### Examples

```
> (succ 1)
2
```

***

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

Get tail.

Produces the tail of a cell.

#### Accepts

A cell.

#### Produces

A `$noun`.

#### Source

```hoon
++  tail  |*(^ ,:+<+)
```

#### Examples

```
> (tail [1 2])
2
```

```
> (tail [[1 1] 2])
2
```

```
> (tail "hello")
"ello"
```

***

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

Test for equality.

Checks if `a` and `b` are equal, producing a `$flag`.

#### Accepts

`a` is a `$noun`.

`b` is a `$noun`.

#### Produces

A `$flag`.

#### Source

```hoon
++  test  |=(^ =(+<- +<+))
```

#### Examples

```
> (test 1 1)
%.y
```

```
> (test [2 0] 2)
%.n
```

```
> (test "hello" 'hello')
%.n
```

```
> (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
%.y
```

***

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

Put head.

`(lead a)` produces a wet gate, then `((lead a) b)` produces `[a b]`.

#### Accepts

`a` is a `$noun`, and is the sample of `+lead`.

`b` is a `$noun`, and is the sample of `((lead a) b)`

#### Produces

A cell of `[a b]`.

#### Source

```hoon
++  lead  |*(* |*(* [+>+< +<]))
```

#### Examples

```
> =a (lead 'foo')

> (a 'bar')
['foo' 'bar']
```

***

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

Put tail.

`(late a)` produces a wet gate, then `((late a) b)` produces the inverted cell `[b a]`. This is the inverted version of [`+lead`](#lead).

#### Accepts

`a` is a `$noun`, and is the sample of `+late`.

`b` is a `$noun`, and is the sample of `(late a)`.

#### Produces

A cell of `[b a]`.

#### Source

```hoon
++  late  |*(* |*(* [+< +>+<]))
```

#### Examples

```
> =a (late 'foo')

> (a 'bar')
['bar' 'foo']
```

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.urbit.org/hoon/stdlib/2n.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
