on f3ca521 help sign in
427 lines 4.8 kB history raw
1+++
2title = "2n: Functional Hacks"
3weight = 17
4template = "doc.html"
5aliases = ["/docs/reference/library/2n"]
6+++
7### `++cork`
9Compose forward
11Build `f` such that `(f x) .= (b (a x))`.
13#### Accepts
15`a` is a noun.
17`b` is a gate.
19#### Source
21```hoon
22 ++ cork |*([a=_|=(* **) b=gate] (corl b a))
23```
25#### Examples
27```
28 > (:(cork dec dec dec) 20)
29 17
31 > =mal (mo (limo a+15 b+23 ~))
32 > ((cork ~(got by mal) dec) %a)
33 14
35 > ((cork ~(got by mal) dec) %b)
36 22
37```
39---
40### `++corl`
42Compose backward
44Build `f` such that
46#### Accepts
48`a` is a gate.
50`b` is a noun.
52#### Source
54```hoon
55 ++ corl
56 |* [a=gate b=_|=(* **)]
57 =< +:|.((a (b))) :: span check
58 |* c=_+<.b
59 (a (b c))
60```
62#### Examples
64```
65 > ((corl (lift bex) (slat %ud)) '2')
66 [~ 4]
67```
69---
70### `++curr`
72Right curry
74Right-curry a gate, binding the tail of its sample
76#### Accepts
78`a` is a gate.
80`c` is a noun.
82#### Produces
84A gate.
86#### Source
88```hoon
89 ++ curr
90 |* [a=_|=(^ **) c=*}
91 |* b=_+<+.a
92 (a b c)
93```
95#### Examples
97```
98 > =tep (curr scan sym)
99 > `@t`(tep "asd")
100 'asd'
102 > `@t`(tep "lek-om")
103 'lek-om'
104```
106---
107### `++cury`
109Curry left
111Curry a gate, binding the head of its sample
113#### Accepts
115`a` is a gate.
117`b` is a noun.
119#### Produces
121A gate.
123#### Source
125```hoon
126 ++ cury
127 |* [a=_|=(^ **) b=*}
128 |* c=_+<+.a
129 (a b c)
130```
132#### Examples
134```
135 > =mol (cury add 2)
136 > (mol 4)
137 6
139 > (mol 7)
140 9
141```
143---
144### `++hard`
146Force remold
148Demands that a specific type be produced, crashing the program if it is
149not.
151#### Source
153```hoon
154 ++ hard
155 |* han=gate
156 |= fud=* ^- han
157 ~_ leaf+"hard"
158 =+ gol=(han fud)
159 ?>(=(gol fud) gol)
160```
162#### Examples
164```
165 > ((hard (list)) (limo [1 2 3 ~]))
166 ~[1 2 3]
167 > ((hard @) (add 2 2))
168 4
169 > ((hard @t) (crip "Tape to cord, bro!"))
170 'Tape to cord, bro'
171 > ((hard tape) (crip "...Tape to cord, bro?..."))
172 ! hard
173 ! exit
174```
177---
178### `++soft`
180Maybe remold
182Politely requests a specific type to be produced, producing null if it
183is not.
185#### Source
187```hoon
188 ++ soft
189 |* han=gate
190 |= fud=* ^- (unit han)
191 =+ gol=(han fud)
192 ?.(=(gol fud) ~ [~ gol])
193```
195#### Examples
197```
198 > ((soft %4) (add 2 2))
199 [~ %4]
201 > ((soft @) (add 2 2))
202 [~ 4]
204 > ((soft %5) (add 2 2))
205 ~
207 > ((soft @t) (crip "Tape to cord, Woohoo!"))
208 [~ 'Tape to cord, Woohoo!']
210 > ((soft @t) (trip 'Cmon man... Tape to cord? Please?!'))
211 ~
212```
215---
216### `++head`
218Get head
220Produces the head of a cell.
222#### Accepts
224A cell.
226#### Produces
228An atom.
230#### Source
232```hoon
233 ++ head |*(^ ,:+<-)
234```
236#### Examples
238```
239 > (head [1 2])
240 1
242 > (head [[1 1] 2])
243 [1 1]
245 > (head "hello")
246 'h'
247```
249---
250### `++same`
252Identity
254Produces the same value that it was given.
256#### Accepts
258A noun.
260#### Produces
262A noun.
264#### Source
266```hoon
267 ++ same |*(* +<)
268```
270#### Examples
272```
273 > (same [1 2])
274 [1 2]
276 > (same [[1 1] 2])
277 [[1 1] 2]
279 > (same "hello")
280 "hello"
281```
283---
284### `++slog`
286Deify printf
288Produces a stack trace if the stack trace isn't null, then produces the other
289input.
291#### Accepts
293A `tang`.
295#### Produces
297A `tang`.
299#### Source
301```hoon
302 ++ slog
303 =| pri=@
304 |= a=tang ^+ same
305 ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
306```
308#### Examples
310```
311 > =+ result=((slog [%leaf "error-message"]~) [12 13]) [%product result]
312 [%product 12 13]
313```
315#### Discussion
317`slog` is intended to be put in the middle of a bunch of chained function
318calls that string a piece of data through them, so that an error message will be
319printed if there's one to print.
321---
322### `++mean`
324Crash and printf
326Ends the program and produces a `tang` tracing error message.
328#### Accepts
330`a` is a tang.
332#### Produces
334A crash.
336#### Source
338```hoon
339 ++ mean
340 |= a=tang
341 ^+ !!
342 ?~ a !!
343 ~_(i.a $(a t.a))
344```
346#### Examples
348```
349 > ~>(%mean.[%leaf "fuse-loop"] !!)
350 ####
351 !! crash
352```
354---
355### `++tail`
357Get tail
359Produces the tail of a cell.
361#### Accepts
363A cell.
365#### Produces
367A noun.
369#### Source
371```hoon
372 ++ tail |*(^ ,:+<+)
373```
375#### Examples
377```
378 > (tail [1 2])
379 2
381 > (tail [[1 1] 2])
382 2
384 > (tail "hello")
385 "ello"
386```
388---
389### `++test`
391Test for equality
393Checks if `a` and `b` are equal, producing a flag.
395#### Accepts
397`a` is a noun.
399`b` is a noun.
401#### Produces
403A flag.
405#### Source
407```hoon
408 ++ test |=(^ =(+<- +<+))
409```
411#### Examples
413```
414 > (test 1 1)
415 %.y
417 > (test [2 0] 2)
418 %.n
420 > (test "hello" 'hello')
421 %.n
423 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
424 %.y
425```
427---