Moritz Heidkamp <mor...@twoticketsplease.de> writes:

> find attached a suggested addition to miscmacros

Attached a syntax-rules based implementation which is much easier to
grok. I am leaning towards putting those into a separate egg now though.

Index: miscmacros.scm
===================================================================
--- miscmacros.scm	(revision 25904)
+++ miscmacros.scm	(working copy)
@@ -9,7 +9,8 @@
    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.
@@ -303,4 +304,32 @@
     clauses ...
     (else (error "no valid case" val))))
 
+(define-syntax ->
+  (syntax-rules ()
+    ((_ x) x)
+    ((_ x (y z ...) rest ...)
+     (-> (y x z ...) rest ...))))
+
+(define-syntax ->>
+  (syntax-rules ()
+    ((_ x) x)
+    ((_ x (y ...) rest ...)
+     (->> (y ... x) rest ...))))
+
+(define-syntax ->*
+  (syntax-rules ()
+    ((_ x) x)
+    ((_ x (y z ...) rest ...)
+     (->* (receive args x
+            (apply y (append args (list z ...))))
+          rest ...))))
+
+(define-syntax ->>*
+  (syntax-rules ()
+    ((_ x) x)
+    ((_ x (y z ...) rest ...)
+     (->>* (receive args x
+             (apply y (append (list z ...) args)))
+           rest ...))))
+
 )
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)))
+
_______________________________________________
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers

Reply via email to