Opened 12 years ago
Last modified 12 years ago
#1020 accepted enhancement
matchable: Add conversion pattern(s) — at Initial Version
Reported by: | Moritz Heidkamp | Owned by: | Alex Shinn |
---|---|---|---|
Priority: | not urgent at all | Milestone: | someday |
Component: | extensions | Version: | 4.8.x |
Keywords: | matchable | Cc: | |
Estimated difficulty: |
Description
The attached patch adds two new pattern types to matchable:
(-> convert pat-1 … pat-n)
(-?> convert pat-1 … pat-n)
These patterns work like the (? predicate pat-1 … pat-n)
pattern
in that convert
must be an expression evaluating to a single
argument function which is applied to the corresponding
value. However, in contrast to ?
, the following patterns
pat-1 … pat-n
are applied to the value _returned_ by
convert
. In addition, -?>
only matches when convert
does not return #f
, so it can be expressed as combination of
->
and ?
like this:
(-> convert (? (lambda (x) x) pat-1 … pat-n))
The rationale is that it allows to match against a converted value and
at the same time being able to bind that value to a variable instead
of having to apply the conversion again in the body. One might argue
that -?>
is not really necessary so I'd be happy with only
adding ->
, too. It should be noted that this patch is not
backwards compatible.
Some examples:
(match '("foo" "10") (("foo" (-?> string->number x)) (+ x x))) ;; => 20 (match '("foo" "bar") (("foo" (-?> string->number x)) (+ x x))) ;; Error: (+) bad argument type: #f (match '("foo" "10") (("foo" (-?> string->number x)) (+ x x)) (else 'nothing)) ;; => 20 (match '("foo" "bar") (("foo" (-?> string->number x)) (+ x x)) (else 'nothing)) ;; => nothing