on c19bc7a help sign in
430 lines 4.7 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'17'
3 title/'2n: Functional Hacks'
4 ==
6;>
8# 2n: functional hacks
10### `++cork`
12Compose forward
14Build `f` such that `(f x) .= (b (a x))`.
16#### Accepts
18`a` is a noun.
20`b` is a gate.
22#### Source
24```
25 ++ cork |*([a=_|=(* **) b=gate] (corl b a))
26```
28#### Examples
30```
31 > (:(cork dec dec dec) 20)
32 17
34 > =mal (mo (limo a+15 b+23 ~))
35 > ((cork ~(got by mal) dec) %a)
36 14
38 > ((cork ~(got by mal) dec) %b)
39 22
40```
42---
43### `++corl`
45Compose backward
47Build `f` such that
49#### Accepts
51`a` is a gate.
53`b` is a noun.
55#### Source
57```
58 ++ corl
59 |* [a=gate b=_|=(* **)]
60 =< +:|.((a (b))) :: span check
61 |* c=_+<.b
62 (a (b c))
63```
65#### Examples
67```
68 > ((corl (lift bex) (slat %ud)) '2')
69 [~ 4]
70```
72---
73### `++curr`
75Right curry
77Right-curry a gate, binding the tail of its sample
79#### Accepts
81`a` is a gate.
83`c` is a noun.
85#### Produces
87A gate.
89#### Source
91```
92 ++ curr
93 |* [a=_|=(^ **) c=*}
94 |* b=_+<+.a
95 (a b c)
96```
98#### Examples
100```
101 > =tep (cury scan sym)
102 > `@t`(tep "asd")
103 'asd'
105 > `@t`(tep "lek-om")
106 'lek-om'
107```
109---
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```
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---
147### `++hard`
149Force remold
151Demands that a specific type be produced, crashing the program if it is
152not.
154#### Source
156```
157 ++ hard
158 |* han=gate
159 |= fud=* ^- han
160 ~_ leaf+"hard"
161 =+ gol=(han fud)
162 ?>(=(gol fud) gol)
163```
165#### Examples
167```
168 > ((hard (list)) (limo [1 2 3 ~]))
169 ~[1 2 3]
170 > ((hard @) (add 2 2))
171 4
172 > ((hard @t) (crip "Tape to cord, bro!"))
173 'Tape to cord, bro'
174 > ((hard tape) (crip "...Tape to cord, bro?..."))
175 ! hard
176 ! exit
177```
180---
181### `++soft`
183Maybe remold
185Politely requests a specific type to be produced, producing null if it
186is not.
188#### Source
190```
191 ++ soft
192 |* han=gate
193 |= fud=* ^- (unit han)
194 =+ gol=(han fud)
195 ?.(=(gol fud) ~ [~ gol])
196```
198#### Examples
200```
201 > ((soft %4) (add 2 2))
202 [~ %4]
204 > ((soft @) (add 2 2))
205 [~ 4]
207 > ((soft %5) (add 2 2))
208 ~
210 > ((soft @t) (crip "Tape to cord, Woohoo!"))
211 [~ 'Tape to cord, Woohoo!']
213 > ((soft @t) (trip 'Cmon man... Tape to cord? Please?!'))
214 ~
215```
218---
219### `++head`
221Get head
223Produces the head of a cell.
225#### Accepts
227A cell.
229#### Produces
231An atom.
233#### Source
235```
236 ++ head |*(^ ,:+<-)
237```
239#### Examples
241```
242 > (head [1 2])
243 1
245 > (head [[1 1] 2])
246 [1 1]
248 > (head "hello")
249 'h'
250```
252---
253### `++same`
255Identity
257Produces the same value that it was given.
259#### Accepts
261A noun.
263#### Produces
265A noun.
267#### Source
269```
270 ++ same |*(* +<)
271```
273#### Examples
275```
276 > (same [1 2])
277 [1 2]
279 > (same [[1 1] 2])
280 [[1 1] 2]
282 > (same "hello")
283 "hello"
284```
286---
287### `++slog`
289Deify printf
291Produces a stack trace if the stack trace isn't null, then produces the other
292input.
294#### Accepts
296A `tang`.
298#### Produces
300A `tang`.
302#### Source
304```
305 ++ slog
306 =| pri=@
307 |= a=tang ^+ same
308 ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
309```
311#### Examples
313```
314 > =+ result=((slog [%leaf "error-message"]~) [12 13]) [%product result]
315 [%product 12 13]
316```
318#### Discussion
320`slog` is intended to be put in the middle of a bunch of chained function
321calls that string a piece of data through them, so that an error message will be
322printed if there's one to print.
324---
325### `++mean`
327Crash and printf
329Ends the program and produces a `tang` tracing error message.
331#### Accepts
333`a` is a tang.
335#### Produces
337A crash.
339#### Source
341```
342 ++ mean
343 |= a=tang
344 ^+ !!
345 ?~ a !!
346 ~_(i.a $(a t.a))
347```
349#### Examples
351```
352 > ~>(%mean.[%leaf "fuse-loop"] !!)
353 ####
354 !! crash
355```
357---
358### `++tail`
360Get tail
362Produces the tail of a cell.
364#### Accepts
366A cell.
368#### Produces
370A noun.
372#### Source
374```
375 ++ tail |*(^ ,:+<+)
376```
378#### Examples
380```
381 > (tail [1 2])
382 2
384 > (tail [[1 1] 2])
385 2
387 > (tail "hello")
388 "ello"
389```
391---
392### `++test`
394Test for equality
396Checks if `a` and `b` are equal, producing a flag.
398#### Accepts
400`a` is a noun.
402`b` is a noun.
404#### Produces
406A flag.
408#### Source
410```
411 ++ test |=(^ =(+<- +<+))
412```
414#### Examples
416```
417 > (test 1 1)
418 %.y
420 > (test [2 0] 2)
421 %.n
423 > (test "hello" 'hello')
424 %.n
426 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
427 %.y
428```
430---