-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.fnl
40 lines (34 loc) · 923 Bytes
/
macros.fnl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
; For lua version after 5.1 change unpack to table.unpack
(fn decorate* [list_of_decorators a_function]
"function composition in a more decorator way fashion"
`(-> ,a_function ,(unpack (icollect [_ v (ipairs list_of_decorators)] `(,v)))))
(fn unless* [condition body1 ...]
"Evaluate body for side-effects only when condition is falsy."
(assert body1 "expected body")
`(if (not ,condition)
(do
,body1
,...)))
(fn protect_bailing_returning [...]
`(let
[(ok# result#)
(pcall
#(do
,(unpack
(icollect
[_ v (ipairs [...])]
v))))]
(values ok# result#)))
(fn protect_bailing_with! [f ...]
`(let
[(ok# result#)
(pcall
#(do
,(unpack
(icollect
[_ v (ipairs [...])]
v))))]
(when (not ok#)
(,f result#))))
{:decorate decorate*
:unless unless*}