on bcfe82c help sign in
428 lines 4.7 kB history raw
1+++
2title = "2n: Functional Hacks"
3weight = 17
4template = "doc.html"
5+++
6# 2n: functional hacks
8### `++cork`
10Compose forward
12Build `f` such that `(f x) .= (b (a x))`.
14#### Accepts
16`a` is a noun.
18`b` is a gate.
20#### Source
22```
23 ++ cork |*([a=_|=(* **) b=gate] (corl b a))
24```
26#### Examples
28```
29 > (:(cork dec dec dec) 20)
30 17
32 > =mal (mo (limo a+15 b+23 ~))
33 > ((cork ~(got by mal) dec) %a)
34 14
36 > ((cork ~(got by mal) dec) %b)
37 22
38```
40---
41### `++corl`
43Compose backward
45Build `f` such that
47#### Accepts
49`a` is a gate.
51`b` is a noun.
53#### Source
55```
56 ++ corl
57 |* [a=gate b=_|=(* **)]
58 =< +:|.((a (b))) :: span check
59 |* c=_+<.b
60 (a (b c))
61```
63#### Examples
65```
66 > ((corl (lift bex) (slat %ud)) '2')
67 [~ 4]
68```
70---
71### `++curr`
73Right curry
75Right-curry a gate, binding the tail of its sample
77#### Accepts
79`a` is a gate.
81`c` is a noun.
83#### Produces
85A gate.
87#### Source
89```
90 ++ curr
91 |* [a=_|=(^ **) c=*}
92 |* b=_+<+.a
93 (a b c)
94```
96#### Examples
98```
99 > =tep (curr scan sym)
100 > `@t`(tep "asd")
101 'asd'
103 > `@t`(tep "lek-om")
104 'lek-om'
105```
107---
108### `++cury`
110Curry left
112Curry a gate, binding the head of its sample
114#### Accepts
116`a` is a gate.
118`b` is a noun.
120#### Produces
122A gate.
124#### Source
126```
127 ++ cury
128 |* [a=_|=(^ **) b=*}
129 |* c=_+<+.a
130 (a b c)
131```
133#### Examples
135```
136 > =mol (cury add 2)
137 > (mol 4)
138 6
140 > (mol 7)
141 9
142```
144---
145### `++hard`
147Force remold
149Demands that a specific type be produced, crashing the program if it is
150not.
152#### Source
154```
155 ++ hard
156 |* han=gate
157 |= fud=* ^- han
158 ~_ leaf+"hard"
159 =+ gol=(han fud)
160 ?>(=(gol fud) gol)
161```
163#### Examples
165```
166 > ((hard (list)) (limo [1 2 3 ~]))
167 ~[1 2 3]
168 > ((hard @) (add 2 2))
169 4
170 > ((hard @t) (crip "Tape to cord, bro!"))
171 'Tape to cord, bro'
172 > ((hard tape) (crip "...Tape to cord, bro?..."))
173 ! hard
174 ! exit
175```
178---
179### `++soft`
181Maybe remold
183Politely requests a specific type to be produced, producing null if it
184is not.
186#### Source
188```
189 ++ soft
190 |* han=gate
191 |= fud=* ^- (unit han)
192 =+ gol=(han fud)
193 ?.(=(gol fud) ~ [~ gol])
194```
196#### Examples
198```
199 > ((soft %4) (add 2 2))
200 [~ %4]
202 > ((soft @) (add 2 2))
203 [~ 4]
205 > ((soft %5) (add 2 2))
206 ~
208 > ((soft @t) (crip "Tape to cord, Woohoo!"))
209 [~ 'Tape to cord, Woohoo!']
211 > ((soft @t) (trip 'Cmon man... Tape to cord? Please?!'))
212 ~
213```
216---
217### `++head`
219Get head
221Produces the head of a cell.
223#### Accepts
225A cell.
227#### Produces
229An atom.
231#### Source
233```
234 ++ head |*(^ ,:+<-)
235```
237#### Examples
239```
240 > (head [1 2])
241 1
243 > (head [[1 1] 2])
244 [1 1]
246 > (head "hello")
247 'h'
248```
250---
251### `++same`
253Identity
255Produces the same value that it was given.
257#### Accepts
259A noun.
261#### Produces
263A noun.
265#### Source
267```
268 ++ same |*(* +<)
269```
271#### Examples
273```
274 > (same [1 2])
275 [1 2]
277 > (same [[1 1] 2])
278 [[1 1] 2]
280 > (same "hello")
281 "hello"
282```
284---
285### `++slog`
287Deify printf
289Produces a stack trace if the stack trace isn't null, then produces the other
290input.
292#### Accepts
294A `tang`.
296#### Produces
298A `tang`.
300#### Source
302```
303 ++ slog
304 =| pri=@
305 |= a=tang ^+ same
306 ?~(a same ~>(%slog.[pri i.a] $(a t.a)))
307```
309#### Examples
311```
312 > =+ result=((slog [%leaf "error-message"]~) [12 13]) [%product result]
313 [%product 12 13]
314```
316#### Discussion
318`slog` is intended to be put in the middle of a bunch of chained function
319calls that string a piece of data through them, so that an error message will be
320printed if there's one to print.
322---
323### `++mean`
325Crash and printf
327Ends the program and produces a `tang` tracing error message.
329#### Accepts
331`a` is a tang.
333#### Produces
335A crash.
337#### Source
339```
340 ++ mean
341 |= a=tang
342 ^+ !!
343 ?~ a !!
344 ~_(i.a $(a t.a))
345```
347#### Examples
349```
350 > ~>(%mean.[%leaf "fuse-loop"] !!)
351 ####
352 !! crash
353```
355---
356### `++tail`
358Get tail
360Produces the tail of a cell.
362#### Accepts
364A cell.
366#### Produces
368A noun.
370#### Source
372```
373 ++ tail |*(^ ,:+<+)
374```
376#### Examples
378```
379 > (tail [1 2])
380 2
382 > (tail [[1 1] 2])
383 2
385 > (tail "hello")
386 "ello"
387```
389---
390### `++test`
392Test for equality
394Checks if `a` and `b` are equal, producing a flag.
396#### Accepts
398`a` is a noun.
400`b` is a noun.
402#### Produces
404A flag.
406#### Source
408```
409 ++ test |=(^ =(+<- +<+))
410```
412#### Examples
414```
415 > (test 1 1)
416 %.y
418 > (test [2 0] 2)
419 %.n
421 > (test "hello" 'hello')
422 %.n
424 > (test "hello" ['h' 'e' 'l' 'l' 'o' ~])
425 %.y
426```
428---