ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 1.4 - (hide annotations)
Tue Mar 12 17:28:17 2002 UTC (12 years, 1 month ago) by kaz
Branch: MAIN
CVS Tags: mcvs-0-9, mcvs-0-8, mcvs-0-11, mcvs-0-10, mcvs-0-12, deferred-adds-branch~branch-point
Branch point for: deferred-adds-branch
Changes since 1.3: +11 -4 lines
* find-bind.lisp (find-bind): Make rem-var optional; sometimes
the user might not care about the remaining unmatched list.
1 kaz 1.1 ;;; This source file is part of the Meta-CVS program,
2     ;;; which is distributed under the GNU license.
3     ;;; Copyright 2002 Kaz Kylheku
5     (provide "find-bind")
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))))))
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.
28     Syntax:
30     find-bind ({:test test-func} {:key key-func} {:take take-func})
31 kaz 1.4 ({rem-var} {(var key {default-value})}*)
32 kaz 1.1 alist {decl}* {form}*
34     var A symbol naming a variable to be bound.
35     key A value to be sought after in the input sequence.
36 kaz 1.2 default-value Optional value to bind to the variable if a match
37 kaz 1.3 is not found, or the variable would otherwise be
38     bound to the value NIL.
39 kaz 1.1 rem-var A symbol naming the variable to be bound to
40     a sequence that has only the unmatched elements
41     from the original sequence.
42     test-func A dyadic comparison function, used to compare
43     elements from the input sequence. Default is #'eql.
44     key-func A monadic function, used to select what part of
45     the sequence elements to compare. Default is to
46     compare the elements themselves.
47     take-func A monadic function, specifies what part of elements
48     to binding to variables (other than rem-var). Default
49     is to take the entire element.
50     sequence The input sequence.
52     Semantics:
54     The specified keys are looked up in the sequence and their corresponding
55 kaz 1.2 variables are bound to the values that are found, or else to their
56 kaz 1.3 default value, or NIL if there is no default value. If NIL happens
57     to be the result of an explict match, the default value is substituted,
58     if specified.
59 kaz 1.1
60     The rem-var variable is bound to a new sequence that is stripped of
61     these previously located elements. The declarations and forms are
62     then evaluated in the lexical environment thus formed.
64     The binding occurs left to right. The first occurence of a value
65     name in the binding list matches the first occurence of the value
66     name in the input sequence. A second occurence of the same value
67     matches a second occurence and so on."
68 kaz 1.4 (when (consp remainder)
69     (push remainder var-specs)
70     (setf remainder nil))
71 kaz 1.1 (let ((vars (mapcar #'first var-specs))
72     (keys (mapcar #'second var-specs))
73 kaz 1.2 (defaults (mapcar #'third var-specs))
74 kaz 1.1 (val-sym (gensym "VALS-"))
75     (rem-sym (gensym "REM-")))
77     `(multiple-value-bind (,val-sym ,rem-sym)
78     (find-bind-extract-vals (list ,@keys) ,sequence
79     ,test ,key ,take)
80 kaz 1.4 (declare (ignorable ,rem-sym))
81 kaz 1.2 (setf ,val-sym (mapcar #'(lambda (x y) (if x x y))
82     ,val-sym (list ,@defaults)))
83 kaz 1.4 ,(if remainder
84     `(destructuring-bind (,remainder ,@vars)
85     (cons ,rem-sym ,val-sym)
86     ,@forms)
87     `(destructuring-bind (,@vars) ,val-sym
88     ,@forms)))))

  ViewVC Help
Powered by ViewVC 1.1.5