chicken-janitors
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-janitors] #1020: matchable: Add conversion pattern(s)


From: Chicken Trac
Subject: [Chicken-janitors] #1020: matchable: Add conversion pattern(s)
Date: Fri, 12 Jul 2013 08:48:47 -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  
 Keywords:  matchable          |  
-------------------------------+--------------------------------------------
 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>
Chicken Scheme <http://www.call-with-current-continuation.org/>
Chicken Scheme is a compiler for the Scheme programming language.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]