1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/f_map_in.htm

80 lines
6.9 KiB
HTML
Raw Normal View History

2024-04-01 10:24:07 +02:00
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Function MAP-INTO</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="f_map.htm">
<LINK REL=UP HREF="c_sequen.htm">
<LINK REL=NEXT HREF="f_reduce.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_map.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_sequen.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_reduce.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="map-into"><I>Function</I> <B>MAP-INTO</B></A> <P>
<P>
<P><B>Syntax:</B><P>
<P>
<B>map-into</B> <I>result-sequence function <TT>&amp;rest</TT> sequences</I> =&gt; <I>result-sequence</I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>result-sequence</I>---a <A REL=DEFINITION HREF="26_glo_p.htm#proper_sequence"><I>proper sequence</I></A>. <P>
<I>function</I>---a <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designator</I></A> for a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> of as many <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A> as there are <I>sequences</I>. <P>
<I>sequence</I>---a <A REL=DEFINITION HREF="26_glo_p.htm#proper_sequence"><I>proper sequence</I></A>. <P>
<P><B>Description:</B><P>
<P>
Destructively modifies <I>result-sequence</I> to contain the results of applying <I>function</I> to each element in the argument <I>sequences</I> in turn. <P>
<I>result-sequence</I> and each element of <I>sequences</I> can each be either a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> or a <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A>. If <I>result-sequence</I> and each element of <I>sequences</I> are not all the same length, the iteration terminates when the shortest <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> (of any of the <I>sequences</I> or the <I>result-sequence</I>) is exhausted. If <I>result-sequence</I> is a <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A> with a <A REL=DEFINITION HREF="26_glo_f.htm#fill_pointer"><I>fill pointer</I></A>, the <A REL=DEFINITION HREF="26_glo_f.htm#fill_pointer"><I>fill pointer</I></A> is ignored when deciding how many iterations to perform, and afterwards the <A REL=DEFINITION HREF="26_glo_f.htm#fill_pointer"><I>fill pointer</I></A> is set to the number of times <I>function</I> was applied. If <I>result-sequence</I> is longer than the shortest element of <I>sequences</I>, extra elements at the end of <I>result-sequence</I> are left unchanged. If <I>result-sequence</I> is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, <A REL=DEFINITION HREF="#map-into"><B>map-into</B></A> immediately returns <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, since <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> is a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> of length zero. <P>
If <I>function</I> has side effects, it can count on being called first on all of the elements with index 0, then on all of those numbered 1, and so on. <P>
<P><B>Examples:</B><P>
<P>
<PRE>
(setq a (list 1 2 3 4) b (list 10 10 10 10)) =&gt; (10 10 10 10)
(map-into a #'+ a b) =&gt; (11 12 13 14)
a =&gt; (11 12 13 14)
b =&gt; (10 10 10 10)
(setq k '(one two three)) =&gt; (ONE TWO THREE)
(map-into a #'cons k a) =&gt; ((ONE . 11) (TWO . 12) (THREE . 13) 14)
(map-into a #'gensym) =&gt; (#:G9090 #:G9091 #:G9092 #:G9093)
a =&gt; (#:G9090 #:G9091 #:G9092 #:G9093)
</PRE>
</TT> <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B><P>
<P>
Should be prepared to signal an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> if <I>result-sequence</I> is not a <A REL=DEFINITION HREF="26_glo_p.htm#proper_sequence"><I>proper sequence</I></A>. Should be prepared to signal an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_tp_err.htm#type-error"><B>type-error</B></A> if <I>sequence</I> is not a <A REL=DEFINITION HREF="26_glo_p.htm#proper_sequence"><I>proper sequence</I></A>. <P>
<P><B>See Also:</B> None.
<P>
<P><B>Notes:</B><P>
<P>
<A REL=DEFINITION HREF="#map-into"><B>map-into</B></A> differs from <A REL=DEFINITION HREF="f_map.htm#map"><B>map</B></A> in that it modifies an existing <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> rather than creating a new one. In addition, <A REL=DEFINITION HREF="#map-into"><B>map-into</B></A> can be called with only two arguments, while <A REL=DEFINITION HREF="f_map.htm#map"><B>map</B></A> requires at least three arguments. <P>
<A REL=DEFINITION HREF="#map-into"><B>map-into</B></A> could be defined by: <P>
<PRE>
(defun map-into (result-sequence function &amp;rest sequences)
(loop for index below (apply #'min
(length result-sequence)
(mapcar #'length sequences))
do (setf (elt result-sequence index)
(apply function
(mapcar #'(lambda (seq) (elt seq index))
sequences))))
result-sequence)
</PRE>
</TT> <P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss239.htm">MAP-INTO:ADD-FUNCTION</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>