on cfa2bed help sign in
426 lines 4.7 kB history raw
1+++
2title = "2n: Functional Hacks"
3weight = 17
4template = "doc.html"
5+++
6### `++cork`
8Compose forward
10Build `f` such that `(f x) .= (b (a x))`.
12#### Accepts
14`a` is a noun.
16`b` is a gate.
18#### Source
20```
21 ++ cork |*([a=_|=(* **) b=gate] (corl b a))
22```
24#### Examples
26```
27 > (:(cork dec dec dec) 20)
28 17
30 > =mal (mo (limo a+15 b+23 ~))
31 > ((cork ~(got by mal) dec) %a)
32 14
34 > ((cork ~(got by mal) dec) %b)
35 22
36```
38---
39### `++corl`
41Compose backward
43Build `f` such that
45#### Accepts
47`a` is a gate.
49`b` is a noun.
51#### Source
53```
54 ++ corl
55 |* [a=gate b=_|=(* **)]
56 =< +:|.((a (b))) :: span check
57 |* c=_+<.b
58 (a (b c))
59```
61#### Examples
63```
64 > ((corl (lift bex) (slat %ud)) '2')
65 [~ 4]
66```
68---
69### `++curr`
71Right curry
73Right-curry a gate, binding the tail of its sample
75#### Accepts
77`a` is a gate.
79`c` is a noun.
81#### Produces
83A gate.
85#### Source
87```
88 ++ curr
89 |* [a=_|=(^ **) c=*}
90 |* b=_+<+.a
91 (a b c)
92```
94#### Examples
96```
97 > =tep (curr scan sym)
98 > `@t`(tep "asd")
99 'asd'
101 > `@t`(tep "lek-om")
102 'lek-om'
103```
105---
106### `++cury`
108Curry left
110Curry a gate, binding the head of its sample
112#### Accepts
114`a` is a gate.
116`b` is a noun.
118#### Produces
120A gate.
122#### Source
124```
125 ++ cury
126 |* [a=_|=(^ **) b=*}
127 |* c=_+<+.a
128 (a b c)
129```
131#### Examples
133```
134 > =mol (cury add 2)
135 > (mol 4)
136 6
138 > (mol 7)
139 9
140```
142---
143### `++hard`
145Force remold
147Demands that a specific type be produced, crashing the program if it is
148not.
150#### Source
152```
153 ++ hard
154 |* han=gate
155 |= fud=* ^- han
156 ~_ leaf+"hard"
157 =+ gol=(han fud)
158 ?>(=(gol fud) gol)
159```
161#### Examples
163```
164 > ((hard (list)) (limo [1 2 3 ~]))
165 ~[1 2 3]
166 > ((hard @) (add 2 2))
167 4
168 > ((hard @t) (crip "Tape to cord, bro!"))
169 'Tape to cord, bro'
170 > ((hard tape) (crip "...Tape to cord, bro?..."))
171 ! hard
172 ! exit
173```
176---
177### `++soft`
179Maybe remold
181Politely requests a specific type to be produced, producing null if it
182is not.
184#### Source
186```
187 ++ soft
188 |* han=gate
189 |= fud=* ^- (unit han)
190 =+ gol=(han fud)
191 ?.(=(gol fud) ~ [~ gol])
192```
194#### Examples
196```
197 > ((soft %4) (add 2 2))
198 [~ %4]
200 > ((soft @) (add 2 2))
201 [~ 4]
203 > ((soft %5) (add 2 2))
204 ~
206 > ((soft @t) (crip "Tape to cord, Woohoo!"))
207 [~ 'Tape to cord, Woohoo!']
209 > ((soft @t) (trip 'Cmon man... Tape to cord? Please?!'))
210 ~
211```
214---
215### `++head`
217Get head
219Produces the head of a cell.
221#### Accepts
223A cell.
225#### Produces
227An atom.
229#### Source
231```
232 ++ head |*(^ ,:+<-)
233```
235#### Examples
237```
238 > (head [1 2])
239 1
241 > (head [[1 1] 2])
242 [1 1]
244 > (head "hello")
245 'h'
246```
248---
249### `++same`
251Identity
253Produces the same value that it was given.
255#### Accepts
257A noun.
259#### Produces
261A noun.
263#### Source
265```
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---
283### `++slog`
285Deify printf
287Produces a stack trace if the stack trace isn't null, then produces the other
288input.
290#### Accepts
292A `tang`.
294#### Produces
296A `tang`.
298#### Source
300```
301 ++ slog
302 =| pri=@
303 |= a=tang ^+ same
304 ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
305```
307#### Examples
309```
310 > =+ result=((slog [%leaf "error-message"]~) [12 13]) [%product result]
311 [%product 12 13]
312```
314#### Discussion
316`slog` is intended to be put in the middle of a bunch of chained function
317calls that string a piece of data through them, so that an error message will be
318printed if there's one to print.
320---
321### `++mean`
323Crash and printf
325Ends the program and produces a `tang` tracing error message.
327#### Accepts
329`a` is a tang.
331#### Produces
333A crash.
335#### Source
337```
338 ++ mean
339 |= a=tang
340 ^+ !!
341 ?~ a !!
342 ~_(i.a $(a t.a))
343```
345#### Examples
347```
348 > ~>(%mean.[%leaf "fuse-loop"] !!)
349 ####
350 !! crash
351```
353---
354### `++tail`
356Get tail
358Produces the tail of a cell.
360#### Accepts
362A cell.
364#### Produces
366A noun.
368#### Source
370```
371 ++ tail |*(^ ,:+<+)
372```
374#### Examples
376```
377 > (tail [1 2])
378 2
380 > (tail [[1 1] 2])
381 2
383 > (tail "hello")
384 "ello"
385```
387---
388### `++test`
390Test for equality
392Checks if `a` and `b` are equal, producing a flag.
394#### Accepts
396`a` is a noun.
398`b` is a noun.
400#### Produces
402A flag.
404#### Source
406```
407 ++ test |=(^ =(+<- +<+))
408```
410#### Examples
412```
413 > (test 1 1)
414 %.y
416 > (test [2 0] 2)
417 %.n
419 > (test "hello" 'hello')
420 %.n
422 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
423 %.y
424```
426---