emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node365.html

93 lines
5 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>B.3. Gatherers</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Gatherers">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html6262 HREF="node366.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html6260 HREF="node362.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html6254 HREF="node364.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html6264 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html6265 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html6263 HREF="node366.html"> Discussion</A>
<B>Up:</B> <A NAME=tex2html6261 HREF="node362.html"> Generators and Gatherers</A>
<B> Previous:</B> <A NAME=tex2html6255 HREF="node364.html"> Generators</A>
<HR> <P>
<H1><A NAME=SECTION003530000000000000000>B.3. Gatherers</A></H1>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
These functions create and process gatherers.
<P>
<BR><b>[Function]</b><BR>
<tt>gatherer <i>collector</i></tt><P>The <i>collector</i> must be a function of type
<tt>(function ((series <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap44337.gif">)) <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap44339.gif">)</tt>. Given this function, <tt>gatherer</tt>
returns a gatherer that accepts elements of type <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap44337.gif"> and returns a final
result of type <IMG ALIGN=BOTTOM ALT="" SRC="_24769_tex2html_wrap44339.gif">. The method for combining elements used by the
gatherer is the same as the one used by the <i>collector</i>.
<P>
<BR><b>[Function]</b><BR>
<tt>next-out <i>gatherer</i> <i>item</i></tt><P>Given a gatherer and a value, <tt>next-out</tt> enters the value into the
gatherer.
<P>
<BR><b>[Function]</b><BR>
<tt>result-of <i>gatherer</i></tt><P><tt>result-of</tt> retrieves the net result from a gatherer. <tt>result-of</tt>
can be applied at any time. However, it is an error to apply
<tt>result-of</tt> twice to the same gatherer or to apply <tt>next-out</tt> to a
gatherer once <tt>result-of</tt> has been applied.
<P><pre>
(let ((g (gatherer #'collect-sum)))
(dolist (i '(1 2 3 4))
(next-out g i)
(if (evenp i) (next-out g (* 10 i))))
(result-of g))
=> 70
</pre><P>
<P>
<BR><b>[Macro]</b><BR>
<tt>gathering ({(<i>var</i> <i>fn</i>)}*) {<i>form</i>}*</tt><P>The first subform must be a list of pairs. The first
element of each pair, <i>var</i>, must be a variable name.
The second element of each pair, <i>fn</i>,
must be a form that when wrapped in <tt>(function ...)</tt> is
acceptable as an argument to <tt>gatherer</tt>. Each symbol is bound to a
gatherer constructed from the corresponding collector. The body
(consisting of the <i>forms</i>) is evaluated in the scope of these bindings.
When this evaluation is complete, <tt>gathering</tt> returns the <tt>result-of</tt> each
gatherer. If there are <b>n</b> pairs in the binding list,
<tt>gathering</tt> returns <b>n</b> values. For example:
<P><pre>
(defun examp (data)
(gathering ((x collect) (y collect-sum))
(iterate ((i (scan data)))
(case (first i)
(:slot (next-out x (second i)))
(:part (dolist (j (second i)) (next-out x j))))
(next-out y (third i)))))
(examp '((:slot a 10) (:part (c d) 40))) => (a c d) and 50
</pre><P>
<P>
As a further illustration of gatherers, consider the following definition for a
simplified version of <tt>gathering</tt> that handles only one binding pair.
<P><pre>
(defmacro simple-gathering (((var collector)) &amp;body body)
`(let ((,var (gatherer (function ,collector))))
,@body
(result-of ,var)))
</pre><P>
The full capabilities of
<tt>gathering</tt> can be supported in much the same way.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html6262 HREF="node366.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html6260 HREF="node362.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html6254 HREF="node364.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html6264 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html6265 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html6263 HREF="node366.html"> Discussion</A>
<B>Up:</B> <A NAME=tex2html6261 HREF="node362.html"> Generators and Gatherers</A>
<B> Previous:</B> <A NAME=tex2html6255 HREF="node364.html"> Generators</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>