Source

clojurian / clojurian-syntax.scm

Full commit
(module clojurian-syntax

(doto -> ->* ->> ->>* if-let if-let*)

(import chicken scheme)

;; originally contributed by Martin DeMello
;; rewritten in terms of syntax-rules by Moritz Heidkamp
(define-syntax doto
  (syntax-rules ()
    ((_ x) x)
    ((_ x (fn args ...) ...)
     (let ((val x))
       (fn val args ...)
       ...
       val))))

(define-syntax ->
  (syntax-rules ()
    ((_ x) x)
    ((_ x (y z ...) rest ...)
     (-> (y x z ...) rest ...))
    ((_ x y rest ...)
     (-> x (y) rest ...))))

(define-syntax ->>
  (syntax-rules ()
    ((_ x) x)
    ((_ x (y ...) rest ...)
     (->> (y ... x) rest ...))
    ((_ x y rest ...)
     (->> x (y) rest ...))))

(define-syntax ->*
  (syntax-rules ()
    ((_ x) x)
    ((_ x (y z ...) rest ...)
     (->* (receive args x
            (apply y (append args (list z ...))))
          rest ...))
    ((_ x y rest ...)
     (->* x (y) rest ...))))

(define-syntax ->>*
  (syntax-rules ()
    ((_ x) x)
    ((_ x (y z ...) rest ...)
     (->>* (receive args x
             (apply y (append (list z ...) args)))
           rest ...))
    ((_ x y rest ...)
     (->>* x (y) rest ...))))

(define-syntax if-let
  (syntax-rules ()
    ((_ (x y) then else)
     (let ((x y))
       (if x then else)))))

(define-syntax if-let*
  (syntax-rules ()
    ((_ ((x y) more ...) then else)
     (car (or (and-let* ((x y) more ...)
                (list then))
              (list else))))))

)