/[meta-cvs]/meta-cvs/F-5AD8C72020BEB5748163790EE9832EC7.lisp
ViewVC logotype

Contents of /meta-cvs/F-5AD8C72020BEB5748163790EE9832EC7.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Mon Mar 11 19:34:50 2002 UTC (12 years, 1 month ago) by kaz
Branch: MAIN
* find-bind.lisp: New file.
(find-bind-extract-vals): New function.
(find-bind): New macro.

* error.lisp (restart-destructure): Macro removed.
(mcvs-error-handler): Uses generic find-bind instead of
restart-destructure.
1 ;;; This source file is part of the Meta-CVS program,
2 ;;; which is distributed under the GNU license.
3 ;;; Copyright 2002 Kaz Kylheku
4
5 (provide "find-bind")
6
7 (defun find-bind-extract-vals (val-list sequence test-func key-func take-func)
8 "This is an internal function that performs the sequence processing
9 that underlies the find-bind macro."
10 (let ((remainder (copy-seq sequence)) matches)
11 (dolist (item val-list (values (nreverse matches) remainder))
12 (let ((match (find item remainder :test test-func :key key-func)))
13 (if match
14 (progn
15 (push (funcall take-func match) matches)
16 (setf remainder (remove item remainder
17 :test test-func :key key-func :count 1)))
18 (push nil matches))))))
19
20 (defmacro find-bind ((&key (test '#'eql)
21 (key '#'(lambda (x) x))
22 (take '#'(lambda (x) x)))
23 (remainder &rest var-specs)
24 sequence &body forms)
25 "Slick binding macro for pulling
26 to lambda variables.
27
28 Syntax:
29
30 find-bind ({:test test-func} {:key key-func} {:take take-func})
31 (rem-var {(var key)}*)
32 alist {decl}* {form}*
33
34 var A symbol naming a variable to be bound.
35 key A value to be sought after in the input sequence.
36 rem-var A symbol naming the variable to be bound to
37 a sequence that has only the unmatched elements
38 from the original sequence.
39 test-func A dyadic comparison function, used to compare
40 elements from the input sequence. Default is #'eql.
41 key-func A monadic function, used to select what part of
42 the sequence elements to compare. Default is to
43 compare the elements themselves.
44 take-func A monadic function, specifies what part of elements
45 to binding to variables (other than rem-var). Default
46 is to take the entire element.
47 sequence The input sequence.
48
49 Semantics:
50
51 The specified keys are looked up in the sequence and their corresponding
52 variables are bound to the values that are found, or else to NIL.
53
54 The rem-var variable is bound to a new sequence that is stripped of
55 these previously located elements. The declarations and forms are
56 then evaluated in the lexical environment thus formed.
57
58 The binding occurs left to right. The first occurence of a value
59 name in the binding list matches the first occurence of the value
60 name in the input sequence. A second occurence of the same value
61 matches a second occurence and so on."
62 (let ((vars (mapcar #'first var-specs))
63 (keys (mapcar #'second var-specs))
64 (val-sym (gensym "VALS-"))
65 (rem-sym (gensym "REM-")))
66
67 `(multiple-value-bind (,val-sym ,rem-sym)
68 (find-bind-extract-vals (list ,@keys) ,sequence
69 ,test ,key ,take)
70 (destructuring-bind (,remainder ,@vars)
71 (cons ,rem-sym ,val-sym)
72 ,@forms))))

  ViewVC Help
Powered by ViewVC 1.1.5