Changeset 40391 in project


Ignore:
Timestamp:
08/25/21 06:18:55 (4 weeks ago)
Author:
Idiomdrottning
Message:

Deprecate match-define

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/5/brev-separate

    r40355 r40391  
    114114
    115115==== match-define
    116 {{matchable}} has {{match-lambda}} and {{match-lambda*}} as shortcuts, and
    117 they’re fantastic, but let’s also add a {{match-define}} as a shortcut
    118 on top of them.
    119 
    120 How about these sorta Haskell-style semantics?
    121 
    122  (match-define
    123   (pat body ...)
    124   ...)
    125 
    126 Here's an example:
    127 
    128  (match-define
    129     ((my-map proc ()) '())
    130     ((my-map proc (x . xs))
    131      (cons (proc x) (my-map proc xs))))
    132  
    133  (my-map - (iota 4))
    134 
    135 â‡’ (0 -1 -2 -3)
    136 
    137 Works with curried define too, of course:
    138 
    139  (match-define
    140     (((my-map proc) ()) '())
    141     (((my-map proc) (x . xs))
    142      (cons (proc x) ((my-map proc) xs))))
    143  
    144  ((my-map -) (iota 4))
    145 
    146 â‡’ (0 -1 -2 -3)
    147 
    148  (map (my-map -) '((1 2 3) (10 20 30) (100 200 300)))
    149 
    150 â‡’ ((-1 -2 -3) (-10 -20 -30) (-100 -200 -300))
    151 
    152 (Limitation: This implementation only destructures the outer argument
    153 list. For future versions it would be awesome to be able to pattern
    154 match on every level.)
    155 
    156 
    157 ==== match-define-closure
    158 Combining {{match-define}} and {{define-closure}} into one glorious
    159 Voltron!
    160 
    161  (match-define-closure bindings (pat body) ...)
    162 
    163 Here's an example:
    164 
    165  (match-define-closure
    166   (x 0)
    167   ((counter) (inc! x))
    168   ((counter 'reset) (set! x 0)))
    169  
    170  (counter) (counter) (counter 'reset) (counter) (counter)
    171 
    172 â‡’ 1 2 1 2
    173 
    174 Here is another example, the arity-table from citeSoftware Design for Flexibility.
    175 
    176  (match-define-closure
    177   (ht (make-hash-table))
    178   ((arity proc) (hash-table-ref ht proc))
    179   ((arity proc a) (hash-table-set! ht proc a)))
    180  
    181  (arity cons 2)
    182  (arity cons)
    183 
    184 â‡’ 2
     116Deprecated♥
     117
     118Calling {{match-define}} directly is deprecated in favor
     119of [[../match-generics|using the macro from match-generics]], which can
     120expand to it or to normal {{define}} as needed. (But {{match-define}} is
     121still used internally to avoid circular dependencies.)
    185122
    186123
Note: See TracChangeset for help on using the changeset viewer.