Add a new syntax macro, compose-if.

The macro resembles (or x (fn x)) except:
1) The value is only evaluated once, and
2) Multiple (single-argument) functions can be chained.

If the original value or the result of any function is #f, the
final value is #f. Otherwise the result is ((compose ,@fns) x).
Short-circuit evaluation is employed.
This commit is contained in:
Jesse D. McDonald 2011-12-08 17:18:32 -06:00
parent 173c117d86
commit 704c473015
1 changed files with 6 additions and 0 deletions

View File

@ -69,4 +69,10 @@
(define-syntax (let/cc var . body)
`(call/cc (lambda (,var) ,@body)))
(define-syntax (compose-if x fn . fns)
(let ([it (gensym)])
(if (pair? fns)
`(let ([,it ,x]) (if ,it (compose-if (,fn ,it) ,@fns) #f))
`(let ([,it ,x]) (if ,it (,fn ,it) #f)))))
; vim:set syntax=scheme sw=2 expandtab: