Index: miscmacros.scm =================================================================== --- miscmacros.scm (revision 25904) +++ miscmacros.scm (working copy) @@ -9,13 +9,14 @@ define-optionals define-parameter define-enum ignore-values ignore-errors ecase - define-syntax-rule) + define-syntax-rule + -> ->* ->> ->>*) (import scheme) ;; No effect -- caller must import these manually. (import (only chicken when unless handle-exceptions let-optionals make-parameter - add1 sub1)) + add1 sub1 define-for-syntax)) ;;; Modify locations, T-like: @@ -303,4 +304,55 @@ clauses ... (else (error "no valid case" val)))) +(define-for-syntax (expand-thrush x weave) + (let loop ((y (cdr x)) (form (car x))) + (if (null? y) + form + (let ((z (car y))) + (loop (cdr y) + (weave z form)))))) + +(define-syntax -> + (ir-macro-transformer + (lambda (x i c) + (expand-thrush + (cdr x) + (lambda (z form) + (cons (car z) + (cons form (cdr z)))))))) + +(define-syntax ->* + (ir-macro-transformer + (lambda (x i c) + (expand-thrush + (cdr x) + (lambda (z form) + `(receive args ,form + (apply + ,(car z) + (append args (list . ,(cdr z)))))))))) + +(define-syntax ->> + (ir-macro-transformer + (lambda (x i c) + (expand-thrush + (cdr x) + (lambda (z form) + (append z (list form))))))) + +(define-syntax ->>* + (ir-macro-transformer + (lambda (x i c) + (expand-thrush + (cdr x) + (lambda (z form) + `(receive args ,form + (apply + ,(car z) + (append (list . ,(cdr z)) args)))))))) + ) + + + + Index: tests/run.scm =================================================================== --- tests/run.scm (revision 0) +++ tests/run.scm (working copy) @@ -0,0 +1,22 @@ +(use test srfi-1 miscmacros) + +(test 1 (-> 99 (/ 11) (/ 9))) + +(test '(1 2 3 4) + (->* (values 1 2) + (list 3) + (append '(4)))) + +(test 7 (-> 10 (- 3))) +(test -7 (->> 10 (- 3))) + +(test 9 (->> 1 (+ 2) (* 3))) + +(test 9 (->> '(1 2 3) + (map add1) + (fold + 0))) + +(test '((foo . 100) (bar . 200)) + (->>* (values '(foo bar) '(100 200)) + (map cons))) +