2n: Functional Hacks

+aftr

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.

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

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

Examples

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

+cork

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.

Accepts

.a is a gate.

.b is a gate.

Source

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

Examples

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

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

+corl

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.

Accepts

.a is a gate.

.b is a gate.

Source

++  corl
  |*  [a=$-(* *) b=$-(* *)]
  =<  +:|.((a (b)))      ::  type check
  =+  c=+<.b
  |@  ++  $  (a (b c))
  --

Examples

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

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

+curr

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

++  curr
  |*  [a=$-(^ *) c=*]
  =+  b=+<+.a
  |@  ++  $  (a b c)
  --

Examples

    > =tep (curr scan sym)
    > `@t`(tep "asd")
    'asd'

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

+cury

Curry left

Curry a gate, binding the head of its sample

Accepts

.a is a gate.

.b is a noun.

Produces

A gate.

Source

++  cury
  |*  [a=$-(^ *) b=*]
  =+  c=+<+.a
  |@  ++  $  (a b c)
  --

Examples

    > =mol (cury add 2)
    > (mol 4)
    6

    > (mol 7)
    9

+fore

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

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

Examples

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

Get head

Produces the head of a cell.

Accepts

A cell.

Produces

A noun.

Source

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

Examples

> (head [1 2])
1

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

> (head "hello")
'h'

+same

Identity

Produces the same value that it was given.

Accepts

A noun.

Produces

A noun.

Source

++  same  |*(* +<)

Examples

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

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

> (same "hello")
"hello"

+succ

Successor

Increment an atom.

Accepts

An $atom.

Produces

An $atom.

Source

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

Examples

> (succ 1)
2

+tail

Get tail

Produces the tail of a cell.

Accepts

A cell.

Produces

A noun.

Source

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

Examples

    > (tail [1 2])
    2

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

    > (tail "hello")
    "ello"

+test

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

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

Examples

    > (test 1 1)
    %.y

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

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

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

+lead

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

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

Examples

> =a (lead 'foo')
> (a 'bar')
['foo' 'bar']

+late

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.

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

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

Examples

> =a (late 'foo')
> (a 'bar')
['bar' 'foo']

Last updated