diff --git a/aand.rkt b/aand.rkt new file mode 100644 index 0000000..de3e3eb --- /dev/null +++ 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 new file mode 100644 index 0000000..28dfac8 --- /dev/null +++ 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 index 7299073..66dc60e 100644 --- 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 index c1f62de..fdb847e 100644 --- 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. +} +