on 883cb21 help sign in
450 lines 4.5 kB history raw
1+++
2title = "2n: Functional Hacks"
3weight = 17
4template = "doc.html"
5+++
7## `++aftr`
9Pair after
11`+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).
13#### Accepts
15`a` is a gate, and the sample of `+aftr`.
17`b` is a gate, and the sample of `(aftr a)`.
19#### Produces
21`(pair b a)`.
23#### Source
25```hoon
26++ aftr |*(a=$-(* *) |*(b=$-(* *) (pair b a)))
27```
29#### Examples
31```
32> =a (aftr @ud)
33> `(a @t)`['foo' 42]
34[p='foo' q=42]
35```
37---
39## `++cork`
41Compose forward
43Build `f` such that `(f x) .= (b (a x))`.
45#### Accepts
47`a` is a noun.
49`b` is a gate.
51#### Source
53```hoon
54++ cork |*([a=$-(* *) b=$-(* *)] (corl b a))
55```
57#### Examples
59```
60 > (:(cork dec dec dec) 20)
61 17
63 > `@ub`(bex (met 0 23))
64 0b10.0000
66 > `@ub`((cork (cury met 0) bex) 23)
67 0b10.0000
68```
70---
72## `++corl`
74Compose backward
76Build `f` such that
78#### Accepts
80`a` is a gate.
82`b` is a noun.
84#### Source
86```hoon
87++ corl
88 |* [a=$-(* *) b=$-(* *)]
89 =< +:|.((a (b))) :: type check
90 =+ c=+<.b
91 |@ ++ $ (a (b c))
92 --
93```
95#### Examples
97```
98 > ((corl (lift bex) (slat %ud)) '2')
99 [~ 4]
100```
102---
104## `++curr` {#curry}
106Right curry
108Right-curry a gate, binding the tail of its sample
110#### Accepts
112`a` is a gate.
114`c` is a noun.
116#### Produces
118A gate.
120#### Source
122```hoon
123++ curr
124 |* [a=$-(^ *) c=*]
125 =+ b=+<+.a
126 |@ ++ $ (a b c)
127 --
128```
130#### Examples
132```
133 > =tep (curr scan sym)
134 > `@t`(tep "asd")
135 'asd'
137 > `@t`(tep "lek-om")
138 'lek-om'
139```
141---
143## `++cury`
145Curry left
147Curry a gate, binding the head of its sample
149#### Accepts
151`a` is a gate.
153`b` is a noun.
155#### Produces
157A gate.
159#### Source
161```hoon
162++ cury
163 |* [a=$-(^ *) b=*]
164 =+ c=+<+.a
165 |@ ++ $ (a b c)
166 --
167```
169#### Examples
171```
172 > =mol (cury add 2)
173 > (mol 4)
174 6
176 > (mol 7)
177 9
178```
180---
182## `++fore`
184Pair before
186`+fore` first takes gate `a`, producing a wet gate. The new wet gate then takes `b`, producing the pair of `[a b]`.
188#### Accepts
190`a` is a gate, and is the sample of `+fore`.
192`b` is a gate, and is the sample of `(fore a)`.
194#### Produces
196`(pair a b)`.
198#### Source
200```hoon
201++ fore |*(a=$-(* *) |*(b=$-(* *) (pair a b)))
202```
204#### Examples
206```
207> =a (fore @ud)
208> `(a @t)`[42 'foo']
209[p=42 q='foo']
210```
212---
214## `++head`
216Get head
218Produces the head of a cell.
220#### Accepts
222A cell.
224#### Produces
226A noun.
228#### Source
230```hoon
231++ head |*(^ ,:+<-)
232```
234#### Examples
236```
237> (head [1 2])
240> (head [[1 1] 2])
241[1 1]
243> (head "hello")
244'h'
245```
247---
249## `++same`
251Identity
253Produces the same value that it was given.
255#### Accepts
257A noun.
259#### Produces
261A noun.
263#### Source
265```hoon
266++ same |*(* +<)
267```
269#### Examples
271```
272> (same [1 2])
273[1 2]
275> (same [[1 1] 2])
276[[1 1] 2]
278> (same "hello")
279"hello"
280```
282---
284## `++succ`
286Successor
288Increment an atom.
290#### Accepts
292An `atom`.
294#### Produces
296An `atom`.
298#### Source
300```hoon
301++ succ |=(@ +(+<))
302```
304#### Examples
306```
307> (succ 1)
309```
311---
313## `++tail`
315Get tail
317Produces the tail of a cell.
319#### Accepts
321A cell.
323#### Produces
325A noun.
327#### Source
329```hoon
330++ tail |*(^ ,:+<+)
331```
333#### Examples
335```
336 > (tail [1 2])
337 2
339 > (tail [[1 1] 2])
340 2
342 > (tail "hello")
343 "ello"
344```
346---
348## `++test`
350Test for equality
352Checks if `a` and `b` are equal, producing a flag.
354#### Accepts
356`a` is a noun.
358`b` is a noun.
360#### Produces
362A flag.
364#### Source
366```hoon
367++ test |=(^ =(+<- +<+))
368```
370#### Examples
372```
373 > (test 1 1)
374 %.y
376 > (test [2 0] 2)
377 %.n
379 > (test "hello" 'hello')
380 %.n
382 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
383 %.y
384```
386---
388## `++lead`
390Put head
392`(lead a)` produces a wet gate, then `((lead a) b)` produces `[a b]`.
394#### Accepts
396`a` is a noun, and is the sample of `+lead`.
398`b` is a noun, and is the sample of `((lead a) b)`
400#### Produces
402A cell of `[a b]`.
404#### Source
406```hoon
407++ lead |*(* |*(* [+>+< +<]))
408```
410#### Examples
412```
413> =a (lead 'foo')
414> (a 'bar')
415['foo' 'bar']
416```
418---
420## `++late`
422Put tail
424`(late a)` produces a wet gate, then `((late a) b)` produces the inverted cell `[b a]`. This is the inverted version of [+lead](#lead).
426#### Accepts
428`a` is a noun, and is the sample of `+late`.
430`b` is a noun, and is the sample of `(late a)`.
432#### Produces
434A cell of `[b a]`.
436#### Source
438```hoon
439++ late |*(* |*(* [+< +>+<]))
440```
442#### Examples
444```
445> =a (late 'foo')
446> (a 'bar')
447['bar' 'foo']
448```
450---