Commits

Moritz Heidkamp  committed b09197c

Add if-let and if-let*

  • Participants
  • Parent commits 73ba2b4

Comments (0)

Files changed (2)

File clojurian-syntax.scm

 (module clojurian-syntax
 
-(doto -> ->* ->> ->>*)
+(doto -> ->* ->> ->>* if-let if-let*)
 
 (import chicken scheme)
 
              (apply y (append (list z ...) args)))
            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)
+     (or (and-let* ((x y) more ...)
+           then)
+         else))))
+
 )

File tests/syntax.scm

 (load-relative "../clojurian-syntax")
 (import clojurian-syntax)
 
-(test (vector 1 2)
-      (doto (make-vector 2)
-            (vector-set! 0 1)
-            (vector-set! 1 2)))
+(test-group "clojurian-syntax"
+  (test (vector 1 2)
+        (doto (make-vector 2)
+              (vector-set! 0 1)
+              (vector-set! 1 2)))
 
-(test 'foo (doto 'foo))
+  (test 'foo (doto 'foo)))
 
-(test 1 (-> 99 (/ 11) (/ 9)))
+(test-group "thrush"
+  (test 1 (-> 99 (/ 11) (/ 9)))
 
-(test '(1 2 3 4)
-      (->* (values 1 2)
-           (list 3)
-           (append '(4))))
+  (test '(1 2 3 4)
+        (->* (values 1 2)
+             (list 3)
+             (append '(4))))
 
-(test 7 (-> 10 (- 3)))
-(test -7 (->> 10 (- 3)))
+  (test 7 (-> 10 (- 3)))
+  (test -7 (->> 10 (- 3)))
 
-(test 9 (->> 1 (+ 2) (* 3)))
+  (test 9 (->> 1 (+ 2) (* 3)))
 
-(test 9 (->> '(1 2 3)
-             (map add1)
-             (fold + 0)))
+  (test 9 (->> '(1 2 3)
+               (map add1)
+               (fold + 0)))
 
-(test '((foo . 100) (bar . 200))
-      (->>* (values '(foo bar) '(100 200))
-            (map cons)))
+  (test '((foo . 100) (bar . 200))
+        (->>* (values '(foo bar) '(100 200))
+              (map cons))))
+
+(test-group "if-let & if-let*"
+  (test 2 (if-let (x 1) (+ x x) 9))
+  (test 9 (if-let (x #f) (+ x x) 9))
+
+  (test (list 3 6)
+        (if-let* ((foo 3) (bar (* foo 2)))
+                 (list foo bar)
+                 'wrong))
+
+  (test 'wrong
+        (if-let* ((foo #f) (bar (* foo 2)))
+                 (list foo bar)
+                 'wrong)))