on 8b27fae help sign in
436 lines 4.8 kB history raw
1+++
2title = "2n: Functional Hacks"
3weight = 17
4template = "doc.html"
5+++
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=$-(* *)] (corl b a))
23```
25#### Examples
27```
28 > (:(cork dec dec dec) 20)
29 17
31 > `@ub`(bex (met 0 23))
32 0b10.0000
34 > `@ub`((cork (cury met 0) bex) 23)
35 0b10.0000
36```
38---
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 :: compose backwards
56 |* [a=$-(* *) b=$-(* *)]
57 =< +:|.((a (b))) :: type check
58 =+ c=+<.b
59 |@ ++ $ (a (b c))
60 --
61```
63#### Examples
65```
66 > ((corl (lift bex) (slat %ud)) '2')
67 [~ 4]
68```
70---
72### `++curr` {#curry}
74Right curry
76Right-curry a gate, binding the tail of its sample
78#### Accepts
80`a` is a gate.
82`c` is a noun.
84#### Produces
86A gate.
88#### Source
90```hoon
91 ++ curr
92 |* [a=_|=(^ **) c=*}
93 |* b=_+<+.a
94 (a b c)
95```
97#### Examples
99```
100 > =tep (curr scan sym)
101 > `@t`(tep "asd")
102 'asd'
104 > `@t`(tep "lek-om")
105 'lek-om'
106```
108---
110### `++cury`
112Curry left
114Curry a gate, binding the head of its sample
116#### Accepts
118`a` is a gate.
120`b` is a noun.
122#### Produces
124A gate.
126#### Source
128```hoon
129 ++ cury
130 |* [a=_|=(^ **) b=*}
131 |* c=_+<+.a
132 (a b c)
133```
135#### Examples
137```
138 > =mol (cury add 2)
139 > (mol 4)
140 6
142 > (mol 7)
143 9
144```
146---
148### `++hard`
150Force remold
152Demands that a specific type be produced, crashing the program if it is
153not.
155#### Source
157```hoon
158 ++ hard
159 |* han=gate
160 |= fud=* ^- han
161 ~_ leaf+"hard"
162 =+ gol=(han fud)
163 ?>(=(gol fud) gol)
164```
166#### Examples
168```
169 > ((hard (list)) (limo [1 2 3 ~]))
170 ~[1 2 3]
171 > ((hard @) (add 2 2))
172 4
173 > ((hard @t) (crip "Tape to cord, bro!"))
174 'Tape to cord, bro'
175 > ((hard tape) (crip "...Tape to cord, bro?..."))
176 ! hard
177 ! exit
178```
180---
182### `++soft`
184Maybe remold
186Politely requests a specific type to be produced, producing null if it
187is not.
189#### Source
191```hoon
192 ++ soft
193 |* han=gate
194 |= fud=* ^- (unit han)
195 =+ gol=(han fud)
196 ?.(=(gol fud) ~ [~ gol])
197```
199#### Examples
201```
202 > ((soft %4) (add 2 2))
203 [~ %4]
205 > ((soft @) (add 2 2))
206 [~ 4]
208 > ((soft %5) (add 2 2))
209 ~
211 > ((soft @t) (crip "Tape to cord, Woohoo!"))
212 [~ 'Tape to cord, Woohoo!']
214 > ((soft @t) (trip 'Cmon man... Tape to cord? Please?!'))
215 ~
216```
218---
220### `++head`
222Get head
224Produces the head of a cell.
226#### Accepts
228A cell.
230#### Produces
232An atom.
234#### Source
236```hoon
237 ++ head |*(^ ,:+<-)
238```
240#### Examples
242```
243 > (head [1 2])
244 1
246 > (head [[1 1] 2])
247 [1 1]
249 > (head "hello")
250 'h'
251```
253---
255### `++same`
257Identity
259Produces the same value that it was given.
261#### Accepts
263A noun.
265#### Produces
267A noun.
269#### Source
271```hoon
272 ++ same |*(* +<)
273```
275#### Examples
277```
278 > (same [1 2])
279 [1 2]
281 > (same [[1 1] 2])
282 [[1 1] 2]
284 > (same "hello")
285 "hello"
286```
288---
290### `++slog`
292Deify printf
294Produces a stack trace if the stack trace isn't null, then produces the other
295input.
297#### Accepts
299A `tang`.
301#### Produces
303A `tang`.
305#### Source
307```hoon
308 ++ slog
309 =| pri=@
310 |= a=tang ^+ same
311 ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
312```
314#### Examples
316```
317 > =+ result=((slog [%leaf "error-message"]~) [12 13]) [%product result]
318 [%product 12 13]
319```
321#### Discussion
323`slog` is intended to be put in the middle of a bunch of chained function
324calls that string a piece of data through them, so that an error message will be
325printed if there's one to print.
327---
329### `++mean`
331Crash and printf
333Ends the program and produces a `tang` tracing error message.
335#### Accepts
337`a` is a tang.
339#### Produces
341A crash.
343#### Source
345```hoon
346 ++ mean
347 |= a=tang
348 ^+ !!
349 ?~ a !!
350 ~_(i.a $(a t.a))
351```
353#### Examples
355```
356 > ~>(%mean.[%leaf "fuse-loop"] !!)
357 ####
358 !! crash
359```
361---
363### `++tail`
365Get tail
367Produces the tail of a cell.
369#### Accepts
371A cell.
373#### Produces
375A noun.
377#### Source
379```hoon
380 ++ tail |*(^ ,:+<+)
381```
383#### Examples
385```
386 > (tail [1 2])
387 2
389 > (tail [[1 1] 2])
390 2
392 > (tail "hello")
393 "ello"
394```
396---
398### `++test`
400Test for equality
402Checks if `a` and `b` are equal, producing a flag.
404#### Accepts
406`a` is a noun.
408`b` is a noun.
410#### Produces
412A flag.
414#### Source
416```hoon
417 ++ test |=(^ =(+<- +<+))
418```
420#### Examples
422```
423 > (test 1 1)
424 %.y
426 > (test [2 0] 2)
427 %.n
429 > (test "hello" 'hello')
430 %.n
432 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
433 %.y
434```
436---