[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s)
From: |
Chicken Trac |
Subject: |
Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s) |
Date: |
Fri, 12 Jul 2013 08:53:02 -0000 |
#1020: matchable: Add conversion pattern(s)
--------------------------------+-------------------------------------------
Reporter: syn | Owner: ashinn
Type: enhancement | Status: new
Priority: not urgent at all | Milestone: someday
Component: extensions | Version: 4.8.x
Resolution: | Keywords: matchable
--------------------------------+-------------------------------------------
Description changed by syn:
Old 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
> }}}
New 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
}}}
--
--
Ticket URL: <http://bugs.call-cc.org/ticket/1020#comment:1>
Chicken Scheme <http://www.call-with-current-continuation.org/>
Chicken Scheme is a compiler for the Scheme programming language.
- [Chicken-janitors] #1020: matchable: Add conversion pattern(s), Chicken Trac, 2013/07/12
- Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s),
Chicken Trac <=
- Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s), Chicken Trac, 2013/07/12
- Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s), Chicken Trac, 2013/07/12
- Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s), Chicken Trac, 2013/07/12
- Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s), Chicken Trac, 2013/07/12
- Re: [Chicken-janitors] #1020: matchable: Add conversion pattern(s), Chicken Trac, 2013/07/13