commit b5b44a35fc18ed0ff7f800adb331240589b6925c
parent affa5e15a0e8dd488b835a77332d3d09bca3b03a
Author: Georges Dupéron <georges.duperon@gmail.com>
Date: Sat, 27 Jan 2018 12:04:31 +0100
Merge branch 'AlexKnauth-and-let'
Diffstat:
4 files changed, 66 insertions(+), 4 deletions(-)
diff --git a/aand.rkt b/aand.rkt
@@ -0,0 +1,14 @@
+#lang racket/base
+
+(provide aand it)
+(require anaphoric/it
+ anaphoric/aif
+ syntax/parse/define
+ (for-syntax racket/base))
+
+(define-syntax aand
+ (syntax-parser
+ [(_) #'#true]
+ [(_ body:expr) #'body]
+ [(_ [variable:id condition:expr] rest ...+)
+ #'(aif condition (and-let rest ...) #false)]))
diff --git a/and-let.rkt b/and-let.rkt
@@ -0,0 +1,15 @@
+#lang racket/base
+
+(provide and-let)
+
+(require syntax/parse/define
+ anaphoric/if-let
+ (for-syntax racket/base))
+
+(define-syntax and-let
+ (syntax-parser
+ [(_) #'#true]
+ [(_ body:expr) #'body]
+ [(_ [variable:id condition:expr] rest ...+)
+ #'(if-let [variable condition] (and-let rest ...) #false)]))
+
diff --git a/main.rkt b/main.rkt
@@ -14,14 +14,18 @@
anaphoric/aif
anaphoric/awhen
anaphoric/acond
+ anaphoric/aand
anaphoric/if-let
anaphoric/when-let
- anaphoric/cond-let))
+ anaphoric/cond-let
+ anaphoric/and-let))
(require anaphoric/it
anaphoric/aif
anaphoric/awhen
anaphoric/acond
+ anaphoric/aand
anaphoric/if-let
anaphoric/when-let
- anaphoric/cond-let)
+ anaphoric/cond-let
+ anaphoric/and-let)
diff --git a/scribblings/anaphoric.scrbl b/scribblings/anaphoric.scrbl
@@ -90,6 +90,24 @@ using @racket[it].
at most once (evaluation stops at the first successful
@racket[conditionᵢ]).}
+@defform[(aand conditionᵢ ... body)]{
+ Variant of @racket[and] which binds @racket[it]
+ to the value of each @racket[conditionᵢ], in scope within the
+ next @racket[conditionᵢ] or @racket[body]. More precisely, the value
+ of each @racket[conditionᵢ] can be referred to as @racket[it] in
+ the following @racketvarfont{conditionᵢ₊₁}, and the value of the last
+ @racket[conditionᵢ] can be referred to as @racket[it] in the
+ @racket[body]. If there are no @racket[conditionᵢ], i.e. when
+ writing, @racket[(aand body)], then @racket[it] retains its original
+ binding (which means that @racket[it] could be unbound, e.g. if no
+ other @racketmodname[anaphoric] form wraps this one).
+
+ Each @racket[condition] is evaluated at most once, and
+ evaluation stops at the first false condition. The
+ @racket[body] is only evaluated when every
+ @racket[conditionᵢ] is successful.
+}
+
@section{The hygienic versions @racket[if-let],
@racket[when-let] and @racket[cond-let]}
@@ -122,4 +140,16 @@ using @racket[it].
Each @racket[conditionᵢ] is evaluated at most once
(evaluation stops at the first successful
- @racket[conditionᵢ]).}
-\ No newline at end of file
+ @racket[conditionᵢ]).}
+
+@defform[(and-let [identifier conditionᵢ] ... body)]{
+ Variant of @racket[and] which binds each @racket[identifier]
+ to the value of its @racket[conditionᵢ], in scope within every
+ @racket[conditionᵢ] afterwards as well as in @racket[body].
+
+ Each @racket[conditionᵢ] is evaluated at most once, and
+ evaluation stops at the first false condition. The
+ @racket[body] is only evaluated when every
+ @racket[conditionᵢ] is successful.
+}
+