my-cond
(require my-cond) | package: my-cond |
source code: https://github.com/AlexKnauth/my-cond
1 my-cond
syntax
(my-cond my-cond-clause ...)
my-cond-clause = normal-cond-clause | for/cond-clause-form | for*/cond-clause-form | (cond-expander-id stuff ...) | cond-expander-id stuff ... | #:def def | #:defs [def ...] | #:let ([var val] ...) | #:begin [def-or-expr ...] normal-cond-clause = [condition-expr then-body ...+] | [else then-body ...+] | [condition-expr => proc-expr] | [condition-expr] for/cond-clause-form = (for/cond-clause (for-clause ...) my-cond-clause ...) for*/cond-clause-form = (for*/cond-clause (for-clause ...) my-cond-clause ...) for-clause = [id seqence-expr] | [(id ...) sequence-expr] | #:when guard-expr | #:unless guard-expr | #:break guard-expr | #:final guard-expr
my-cond also allows easy internal definitions with things like #:defs [def ...] and #:let ([var val] ...).
As soon as one of the conditions evaluates to a true value, it returns whatever cond would return as the result of that clause.
Otherwise, it goes on to the next clause, if there is one.
If it reaches the end of a my-cond expression where none of the conditions returned a true value, the my-cond expression returns #<void>.
If it reaches the end of a for/cond-clause or for*/cond-clause form, then it goes through the for/cond-clause form again for the next iteration (if there is one).
If there is no "next iteration," then it goes on to the next clause after the for/cond-clause form (if there is one).
An else clause can only appear as the last clause of a my-cond (or cond) form, and cannot appear inside of a for/cond-clause or for*/cond-clause form.
> (require my-cond)
> (my-cond (for/cond-clause ([i (in-range 0 5)]) [(<= 3 i) i] [(<= 2 i) (number->string i)])) "2"
> (my-cond (for/cond-clause ([i (in-range 0 5)]) [(<= 2 i) i] [(<= 3 i) (number->string i)])) 2
syntax
(for/cond-clause (for-clause ...) my-cond-clause ...)
syntax
(for*/cond-clause (for-clause ...) my-cond-clause ...)
2 cond-expanders
procedure
(cond-expander proc) → cond-expander?
proc : (syntax? . -> . syntax)
> (require my-cond racket/match (for-syntax racket/base))
> (define-syntax $m (cond-expander (syntax-rules () [(my-cond [$m val pat body ...] clause ...) (match val [pat body ...] [_ (my-cond clause ...)])])))
> (define x '(1 2 3))
> (my-cond [$m x (list a b c) (+ a b c)] [else #f]) 6
> (my-cond [$m 5 (list a b c) (+ a b c)] [else #f]) #f
value
: (struct-type-property/c (-> cond-expander? (-> syntax? syntax?)))
procedure
(cond-expander? v) → boolean?
v : any/c
procedure
(syntax-local-cond-introduce stx) → syntax?
stx : syntax?