on master help sign in
Update 2n
27fd41drmariani
docs/hoon/library/2n.md+244−60
@@ -4,157 +4,151 @@ navhome: /docs/
4
4
5
5
6# 2n: functional hacks
6# 2n: functional hacks
7+
7### `++cork`
8### `++cork`
8
9
10+Compose forward
11+
9Build `f` such that `(f x) .= (b (a x))`.
12Build `f` such that `(f x) .= (b (a x))`.
10
13
14+#### Accepts
15+
11`a` is a noun.
16`a` is a noun.
12
17
13`b` is a gate.
18`b` is a gate.
14
19
15−Source
20+#### Source
16−------
17−
18− ++ cork |*({a/_|=(* **) b/gate} (corl b a)) :: compose forward
19
21
22+ ++ cork |*([a=_|=(* **) b=gate] (corl b a))
20
23
21−Examples
24+#### Examples
22−--------
23
25
24 > (:(cork dec dec dec) 20)
26 > (:(cork dec dec dec) 20)
25 17
27 17
28+
26 > =mal (mo (limo a+15 b+23 ~))
29 > =mal (mo (limo a+15 b+23 ~))
27 > ((cork ~(got by mal) dec) %a)
30 > ((cork ~(got by mal) dec) %a)
28 14
31 14
32+
29 > ((cork ~(got by mal) dec) %b)
33 > ((cork ~(got by mal) dec) %b)
30 22
34 22
31
35
32
36
33−
34***
37***
35### `++corl`
38### `++corl`
36
39
37−Gate compose
40+Compose backward
38
41
39−XX Revisit
42+Build `f` such that
43+
44+#### Accepts
40
45
41`a` is a gate.
46`a` is a gate.
42
47
43`b` is a noun.
48`b` is a noun.
44
49
45−Source
50+#### Source
46−------
47
51
48− ++ corl :: compose backwards
52+ ++ corl
49− |* {a/gate b/_|=(* **)}
53+ |* [a=gate b=_|=(* **)]
50 =< +:|.((a (b))) :: span check
54 =< +:|.((a (b))) :: span check
51− |* c/_+<.b
55+ |* c=_+<.b
52 (a (b c))
56 (a (b c))
53 ::
57 ::
54
58
55−Examples
59+#### Examples
56−--------
57
60
58 > ((corl (lift bex) (slat %ud)) '2')
61 > ((corl (lift bex) (slat %ud)) '2')
59 [~ 4]
62 [~ 4]
60
63
61
64
62−
63***
65***
64### `++curr`
66### `++curr`
65
67
66Right curry
68Right curry
67
69
68−Right curry a gate, binding the tail of its sample
70+Right-curry a gate, binding the tail of its sample
69
71
70−Accepts
72+#### Accepts
71−-------
72
73
73`a` is a gate.
74`a` is a gate.
74
75
75`c` is a noun.
76`c` is a noun.
76
77
77−Produces
78+#### Produces
78−--------
79
79
80+A gate.
80
81
81−Source
82+#### Source
82−------
83
83
84− ++ curr :: curry right
84+ ++ curr
85− |* {a/_|=(^ **) c/*}
85+ |* [a=_|=(^ **) c=*}
86− |* b/_+<+.a
86+ |* b=_+<+.a
87 (a b c)
87 (a b c)
88− ::
89
88
90−Examples
89+#### Examples
91−--------
92
90
93− > =tep (curr scan sym)
91+ > =tep (cury scan sym)
94 > `@t`(tep "asd")
92 > `@t`(tep "asd")
95 'asd'
93 'asd'
94+
96 > `@t`(tep "lek-om")
95 > `@t`(tep "lek-om")
97 'lek-om'
96 'lek-om'
98
97
99
98
100−
101***
99***
102### `++cury`
100### `++cury`
103
101
102+Curry left
103+
104Curry a gate, binding the head of its sample
104Curry a gate, binding the head of its sample
105
105
106−Accepts
106+#### Accepts
107−-------
108
107
109`a` is a gate.
108`a` is a gate.
110
109
111`b` is a noun.
110`b` is a noun.
112
111
113−Produces
112+#### Produces
114−--------
115
113
116A gate.
114A gate.
117
115
118−Source
116+#### Source
119−------
120
117
121− ++ cury :: curry left
118+ ++ cury
122− |* {a/_|=(^ **) b/*}
119+ |* [a=_|=(^ **) b=*}
123− |* c/_+<+.a
120+ |* c=_+<+.a
124 (a b c)
121 (a b c)
125− ::
126
122
127
123
128−Examples
124+#### Examples
129−--------
130
125
131 > =mol (cury add 2)
126 > =mol (cury add 2)
132 > (mol 4)
127 > (mol 4)
133 6
128 6
129+
134 > (mol 7)
130 > (mol 7)
135 9
131 9
136
132
137−
138−
139***
133***
140### `++hard`
134### `++hard`
141
135
136+Force remold
137+
142Demands that a specific type be produced, crashing the program if it is
138Demands that a specific type be produced, crashing the program if it is
143not.
139not.
144
140
145−Source
141+#### Source
146−------
147
142
148− ++ hard :: force remold
143+ ++ hard
149− |* han/gate
144+ |* han=gate
150− |= fud/* ^- han
145+ |= fud=* ^- han
151 ~_ leaf+"hard"
146 ~_ leaf+"hard"
152 =+ gol=(han fud)
147 =+ gol=(han fud)
153 ?>(=(gol fud) gol)
148 ?>(=(gol fud) gol)
154 ::
149 ::
155
150
156−Examples
151+#### Examples
157−--------
158
152
159 > ((hard (list)) (limo [1 2 3 ~]))
153 > ((hard (list)) (limo [1 2 3 ~]))
160 ~[1 2 3]
154 ~[1 2 3]
@@ -171,21 +165,21 @@ Examples
171***
165***
172### `++soft`
166### `++soft`
173
167
168+Maybe remold
169+
174Politely requests a specific type to be produced, producing null if it
170Politely requests a specific type to be produced, producing null if it
175is not.
171is not.
176
172
177−Source
173+#### Source
178−------
179
174
180− ++ soft :: maybe remold
175+ ++ soft
181− |* han/gate
176+ |* han=gate
182− |= fud/* ^- (unit han)
177+ |= fud=* ^- (unit han)
183 =+ gol=(han fud)
178 =+ gol=(han fud)
184 ?.(=(gol fud) ~ [~ gol])
179 ?.(=(gol fud) ~ [~ gol])
185 ::
180 ::
186
181
187−Examples
182+#### Examples
188−--------
189
183
190 > ((soft %4) (add 2 2))
184 > ((soft %4) (add 2 2))
191 [~ %4]
185 [~ %4]
@@ -200,4 +194,194 @@ Examples
200
194
201
195
202
196
197+***
198+### `++head`
199+
200+Get head
201+
202+Produces the head of a cell.
203+
204+#### Accepts
205+
206+A cell.
207+
208+#### Produces
209+
210+An atom.
211+
212+#### Source
213+
214+ ++ head |*(^ ,:+<-)
215+
216+#### Examples
217+
218+ > (head [1 2])
219+ 1
220+
221+ > (head [[1 1] 2])
222+ [1 1]
223+
224+ > (head "hello")
225+ 'h'
226+
227+
228+***
229+### `++same`
230+
231+Identity
232+
233+Produces the same value that it was given.
234+
235+#### Accepts
236+
237+A noun.
238+
239+#### Produces
240+
241+A noun.
242+
243+#### Source
244+
245+ ++ same |*(* +<)
246+
247+#### Examples
248+
249+ > (same [1 2])
250+ [1 2]
251+
252+ > (same [[1 1] 2])
253+ [[1 1] 2]
254+
255+ > (same "hello")
256+ "hello"
257+
258+***
259+### `++slog`
260+
261+Deify printf
262+
263+Produces a stack trace if the stack trace isn't null, then produces the other
264+input.
265+
266+#### Accepts
267+
268+A `tang`.
269+
270+#### Produces
271+
272+A `tang`.
273+
274+#### Source
275+
276+ ++ slog
277+ =| pri=@
278+ |= a=tang ^+ same
279+ ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
280+
281+#### Examples
282+
283+ > =+ result=((slog [%leaf "error-message"]~) [12 13]) [%product result]
284+ [%product 12 13]
285+
286+#### Discussion
287+
288+`slog` is intended to be put in the middle of a bunch of chained function
289+calls that string a piece of data through them, so that an error message will be
290+printed if there's one to print.
291+
292+***
293+### `++mean`
294+
295+Crash and printf
296+
297+Ends the program and produces a `tang` tracing error message.
298+
299+#### Accepts
300+
301+`a` is a tang.
302+
303+#### Produces
304+
305+A crash.
306+
307+#### Source
308+
309+ ++ mean
310+ |= a=tang
311+ ^+ !!
312+ ?~ a !!
313+ ~_(i.a $(a t.a))
314+
315+#### Examples
316+
317+ > ~>(%mean.[%leaf "fuse-loop"] !!)
318+ ####
319+ !! crash
320+
321+***
322+### `++tail`
323+
324+Get tail
325+
326+Produces the tail of a cell.
327+
328+#### Accepts
329+
330+A cell.
331+
332+#### Produces
333+
334+A noun.
335+
336+#### Source
337+
338+ ++ tail |*(^ ,:+<+)
339+
340+#### Examples
341+
342+ > (tail [1 2])
343+ 2
344+
345+ > (tail [[1 1] 2])
346+ 2
347+
348+ > (tail "hello")
349+ "ello"
350+
351+
352+***
353+### `++test`
354+
355+Test for equality
356+
357+Checks if `a` and `b` are equal, producing a flag.
358+
359+#### Accepts
360+
361+`a` is a noun.
362+
363+`b` is a noun.
364+
365+#### Produces
366+
367+A flag.
368+
369+#### Source
370+
371+ ++ test |=(^ =(+<- +<+))
372+
373+#### Examples
374+
375+ > (test 1 1)
376+ %.y
377+
378+ > (test [2 0] 2)
379+ %.n
380+
381+ > (test "hello" 'hello')
382+ %.n
383+
384+ > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
385+ %.y
386+
203***
387***