on dffd855 help sign in
458 lines 4.8 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
43Call gate `a`, then call gate `b` with its product.
45This is a wet gate that takes two gates and produces a new gate.
47This is the inverse of [`++corl`](#corl).
49#### Accepts
51`a` is a gate.
53`b` is a gate.
55#### Source
57```hoon
58++ cork |*([a=$-(* *) b=$-(* *)] (corl b a))
59```
61#### Examples
63```
64> ((cork dec |=(a=@ [a a])) 20)
65[19 19]
67> ((cork dec some) 20)
68[~ u=19]
69```
71---
73## `++corl`
75Compose backward
77Call gate `b`, then call gate `a` with its product.
79This is a wet gate that takes two gates and produces a new gate.
81This is the inverse of [`++cork`](#cork).
83#### Accepts
85`a` is a gate.
87`b` is a gate.
89#### Source
91```hoon
92++ corl
93 |* [a=$-(* *) b=$-(* *)]
94 =< +:|.((a (b))) :: type check
95 =+ c=+<.b
96 |@ ++ $ (a (b c))
97 --
98```
100#### Examples
102```
103> ((corl |=(a=@ [a a]) dec) 20)
104[19 19]
106> ((corl some dec) 20)
107[~ u=19]
108```
110---
112## `++curr` {% #curry %}
114Right curry
116Right-curry a gate, binding the tail of its sample
118#### Accepts
120`a` is a gate.
122`c` is a noun.
124#### Produces
126A gate.
128#### Source
130```hoon
131++ curr
132 |* [a=$-(^ *) c=*]
133 =+ b=+<+.a
134 |@ ++ $ (a b c)
135 --
136```
138#### Examples
140```
141 > =tep (curr scan sym)
142 > `@t`(tep "asd")
143 'asd'
145 > `@t`(tep "lek-om")
146 'lek-om'
147```
149---
151## `++cury`
153Curry left
155Curry a gate, binding the head of its sample
157#### Accepts
159`a` is a gate.
161`b` is a noun.
163#### Produces
165A gate.
167#### Source
169```hoon
170++ cury
171 |* [a=$-(^ *) b=*]
172 =+ c=+<+.a
173 |@ ++ $ (a b c)
174 --
175```
177#### Examples
179```
180 > =mol (cury add 2)
181 > (mol 4)
182 6
184 > (mol 7)
185 9
186```
188---
190## `++fore`
192Pair before
194`+fore` first takes gate `a`, producing a wet gate. The new wet gate then takes `b`, producing the pair of `[a b]`.
196#### Accepts
198`a` is a gate, and is the sample of `+fore`.
200`b` is a gate, and is the sample of `(fore a)`.
202#### Produces
204`(pair a b)`.
206#### Source
208```hoon
209++ fore |*(a=$-(* *) |*(b=$-(* *) (pair a b)))
210```
212#### Examples
214```
215> =a (fore @ud)
216> `(a @t)`[42 'foo']
217[p=42 q='foo']
218```
220---
222## `++head`
224Get head
226Produces the head of a cell.
228#### Accepts
230A cell.
232#### Produces
234A noun.
236#### Source
238```hoon
239++ head |*(^ ,:+<-)
240```
242#### Examples
244```
245> (head [1 2])
248> (head [[1 1] 2])
249[1 1]
251> (head "hello")
252'h'
253```
255---
257## `++same`
259Identity
261Produces the same value that it was given.
263#### Accepts
265A noun.
267#### Produces
269A noun.
271#### Source
273```hoon
274++ same |*(* +<)
275```
277#### Examples
279```
280> (same [1 2])
281[1 2]
283> (same [[1 1] 2])
284[[1 1] 2]
286> (same "hello")
287"hello"
288```
290---
292## `++succ`
294Successor
296Increment an atom.
298#### Accepts
300An `atom`.
302#### Produces
304An `atom`.
306#### Source
308```hoon
309++ succ |=(@ +(+<))
310```
312#### Examples
314```
315> (succ 1)
317```
319---
321## `++tail`
323Get tail
325Produces the tail of a cell.
327#### Accepts
329A cell.
331#### Produces
333A noun.
335#### Source
337```hoon
338++ tail |*(^ ,:+<+)
339```
341#### Examples
343```
344 > (tail [1 2])
345 2
347 > (tail [[1 1] 2])
348 2
350 > (tail "hello")
351 "ello"
352```
354---
356## `++test`
358Test for equality
360Checks if `a` and `b` are equal, producing a flag.
362#### Accepts
364`a` is a noun.
366`b` is a noun.
368#### Produces
370A flag.
372#### Source
374```hoon
375++ test |=(^ =(+<- +<+))
376```
378#### Examples
380```
381 > (test 1 1)
382 %.y
384 > (test [2 0] 2)
385 %.n
387 > (test "hello" 'hello')
388 %.n
390 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
391 %.y
392```
394---
396## `++lead`
398Put head
400`(lead a)` produces a wet gate, then `((lead a) b)` produces `[a b]`.
402#### Accepts
404`a` is a noun, and is the sample of `+lead`.
406`b` is a noun, and is the sample of `((lead a) b)`
408#### Produces
410A cell of `[a b]`.
412#### Source
414```hoon
415++ lead |*(* |*(* [+>+< +<]))
416```
418#### Examples
420```
421> =a (lead 'foo')
422> (a 'bar')
423['foo' 'bar']
424```
426---
428## `++late`
430Put tail
432`(late a)` produces a wet gate, then `((late a) b)` produces the inverted cell `[b a]`. This is the inverted version of [+lead](#lead).
434#### Accepts
436`a` is a noun, and is the sample of `+late`.
438`b` is a noun, and is the sample of `(late a)`.
440#### Produces
442A cell of `[b a]`.
444#### Source
446```hoon
447++ late |*(* |*(* [+< +>+<]))
448```
450#### Examples
452```
453> =a (late 'foo')
454> (a 'bar')
455['bar' 'foo']
456```
458---