Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/doc/html/lambda/s03.html @ 25

Last change on this file since 25 was 12, checked in by landauf, 17 years ago

added boost

File size: 11.7 KB
Line 
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4<title>Introduction</title>
5<link rel="stylesheet" href="../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.69.1">
7<link rel="start" href="../index.html" title="The Boost C++ Libraries">
8<link rel="up" href="../lambda.html" title="Chapter 6. Boost.Lambda">
9<link rel="prev" href="getting_started.html" title="Getting Started">
10<link rel="next" href="using_library.html" title="Using the library">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%">
14<td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../../boost.png"></td>
15<td align="center"><a href="../../../index.htm">Home</a></td>
16<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="../../../people/people.htm">People</a></td>
18<td align="center"><a href="../../../more/faq.htm">FAQ</a></td>
19<td align="center"><a href="../../../more/index.htm">More</a></td>
20</table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="getting_started.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="using_library.html"><img src="../images/next.png" alt="Next"></a>
24</div>
25<div class="section" lang="en">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="id2707346"></a>Introduction</h3></div></div></div>
28<div class="toc"><dl>
29<dt><span class="section"><a href="s03.html#id2707350">Motivation</a></span></dt>
30<dt><span class="section"><a href="s03.html#id2707599">Introduction to lambda expressions</a></span></dt>
31</dl></div>
32<div class="section" lang="en">
33<div class="titlepage"><div><div><h4 class="title">
34<a name="id2707350"></a>Motivation</h4></div></div></div>
35<p>The Standard Template Library (STL)
36        [<a href="../lambda.html#cit:stepanov:94" title="[STL94]"><span class="abbrev">STL94</span></a>], now part of the C++ Standard Library [<a href="../lambda.html#cit:c++:98" title="[C++98]"><span class="abbrev">C++98</span></a>], is a generic container and algorithm library.
37Typically STL algorithms operate on container elements via <span class="emphasis"><em>function objects</em></span>. These function objects are passed as arguments to the algorithms.
38</p>
39<p>
40Any C++ construct that can be called with the function call syntax
41is a function object.
42The STL contains predefined function objects for some common cases (such as <code class="literal">plus</code>, <code class="literal">less</code> and <code class="literal">not1</code>).
43As an example, one possible implementation for the standard <code class="literal">plus</code> template is:
44
45</p>
46<pre class="programlisting">
47template &lt;class T&gt; : public binary_function&lt;T, T, T&gt;
48struct plus {
49  T operator()(const T&amp; i, const T&amp; j) const {
50    return i + j;
51  }
52};
53</pre>
54<p>
55
56The base class <code class="literal">binary_function&lt;T, T, T&gt;</code> contains typedefs for the argument and return types of the function object, which are needed to make the function object <span class="emphasis"><em>adaptable</em></span>.
57</p>
58<p>
59In addition to the basic function object classes, such as the one above,
60the STL contains <span class="emphasis"><em>binder</em></span> templates for creating a unary function object from an adaptable binary function object by fixing one of the arguments to a constant value.
61For example, instead of having to explicitly write a function object class like:
62
63</p>
64<pre class="programlisting">
65class plus_1 {
66  int _i;
67public:
68  plus_1(const int&amp; i) : _i(i) {}
69  int operator()(const int&amp; j) { return _i + j; }
70};
71</pre>
72<p>
73
74the equivalent functionality can be achieved with the <code class="literal">plus</code> template and one of the binder templates (<code class="literal">bind1st</code>).
75E.g., the following two expressions create function objects with identical functionalities;
76when invoked, both return the result of adding <code class="literal">1</code> to the argument of the function object:
77
78</p>
79<pre class="programlisting">
80plus_1(1)
81bind1st(plus&lt;int&gt;(), 1)
82</pre>
83<p>
84
85The subexpression <code class="literal">plus&lt;int&gt;()</code> in the latter line is a binary function object which computes the sum of two integers, and <code class="literal">bind1st</code> invokes this function object partially binding the first argument to <code class="literal">1</code>.
86As an example of using the above function object, the following code adds <code class="literal">1</code> to each element of some container <code class="literal">a</code> and outputs the results into the standard output stream <code class="literal">cout</code>.
87
88</p>
89<pre class="programlisting">
90transform(a.begin(), a.end(), ostream_iterator&lt;int&gt;(cout),
91          bind1st(plus&lt;int&gt;(), 1));
92</pre>
93<p>
94To make the binder templates more generally applicable, the STL contains <span class="emphasis"><em>adaptors</em></span> for making
95pointers or references to functions, and pointers to member functions,
96adaptable.
97
98Finally, some STL implementations contain function composition operations as
99extensions to the standard [<a href="../lambda.html#cit:sgi:02" title="[SGI02]"><span class="abbrev">SGI02</span></a>].
100      </p>
101<p>
102All these tools aim at one goal: to make it possible to specify
103<span class="emphasis"><em>unnamed functions</em></span> in a call of an STL algorithm,
104in other words, to pass code fragments as an argument to a function.
105
106However, this goal is attained only partially.
107The simple example above shows that the definition of unnamed functions
108with the standard tools is cumbersome.
109
110Complex expressions involving functors, adaptors, binders and
111function composition operations tend to be difficult to comprehend.
112
113In addition to this, there are significant restrictions in applying
114the standard tools. E.g. the standard binders allow only one argument
115of a binary function to be bound; there are no binders for
1163-ary, 4-ary etc. functions.
117</p>
118<p>
119The Boost Lambda Library provides solutions for the problems described above:
120
121</p>
122<div class="itemizedlist"><ul type="disc">
123<li>
124<p>
125Unnamed functions can be created easily with an intuitive syntax.
126
127The above example can be written as:
128
129</p>
130<pre class="programlisting">
131transform(a.begin(), a.end(), ostream_iterator&lt;int&gt;(cout),
132          1 + _1);
133</pre>
134<p>
135
136or even more intuitively:
137
138</p>
139<pre class="programlisting">
140for_each(a.begin(), a.end(), cout &lt;&lt; (1 + _1));
141</pre>
142</li>
143<li><p>
144Most of the restrictions in argument binding are removed,
145arbitrary arguments of practically any C++ function can be bound.
146</p></li>
147<li><p>
148Separate function composition operations are not needed,
149as function composition is supported implicitly.
150
151</p></li>
152</ul></div>
153</div>
154<div class="section" lang="en">
155<div class="titlepage"><div><div><h4 class="title">
156<a name="id2707599"></a>Introduction to lambda expressions</h4></div></div></div>
157<div class="toc"><dl>
158<dt><span class="section"><a href="s03.html#lambda.partial_function_application">Partial function application</a></span></dt>
159<dt><span class="section"><a href="s03.html#lambda.terminology">Terminology</a></span></dt>
160</dl></div>
161<p>
162        Lambda expression are common in functional programming languages.
163        Their syntax varies between languages (and between different forms of lambda calculus), but the basic form of a lambda expressions is:
164
165
166</p>
167<pre class="programlisting">
168lambda x<sub>1</sub> ... x<sub>n</sub>.e
169</pre>
170<p>
171
172        A lambda expression defines an unnamed function and consists of:
173        </p>
174<div class="itemizedlist"><ul type="disc">
175<li><p>
176              the parameters of this function: <code class="literal">x<sub>1</sub> ... x<sub>n</sub></code>.
177              </p></li>
178<li><p>the expression e which computes the value of the function in terms of the parameters <code class="literal">x<sub>1</sub> ... x<sub>n</sub></code>.
179            </p></li>
180</ul></div>
181<p>
182
183        A simple example of a lambda expression is
184</p>
185<pre class="programlisting">
186lambda x y.x+y
187</pre>
188<p>
189Applying the lambda function means substituting the formal parameters with the actual arguments:
190</p>
191<pre class="programlisting">
192(lambda x y.x+y) 2 3 = 2 + 3 = 5
193</pre>
194<p>
195In the C++ version of lambda expressions the <code class="literal">lambda x<sub>1</sub> ... x<sub>n</sub></code> part is missing and the formal parameters have predefined names.
196In the current version of the library,
197there are three such predefined formal parameters,
198called <span class="emphasis"><em>placeholders</em></span>:
199<code class="literal">_1</code>, <code class="literal">_2</code> and <code class="literal">_3</code>.
200They refer to the first, second and third argument of the function defined
201by the lambda expression.
202       
203For example, the C++ version of the definition
204</p>
205<pre class="programlisting">lambda x y.x+y</pre>
206<p>
207is
208</p>
209<pre class="programlisting">_1 + _2</pre>
210<p>
211Hence, there is no syntactic keyword for C++ lambda expressions.
212        The use of a placeholder as an operand implies that the operator invocation is a lambda expression.
213        However, this is true only for operator invocations.
214        Lambda expressions containing function calls, control structures, casts etc. require special syntactic constructs.
215        Most importantly, function calls need to be wrapped inside a <code class="literal">bind</code> function.
216
217        As an example, consider the lambda expression:
218
219        </p>
220<pre class="programlisting">lambda x y.foo(x,y)</pre>
221<p>
222
223        Rather than <code class="literal">foo(_1, _2)</code>, the C++ counterpart for this expression is:
224
225        </p>
226<pre class="programlisting">bind(foo, _1, _2)</pre>
227<p>
228
229        We refer to this type of C++ lambda expressions as <span class="emphasis"><em>bind expressions</em></span>.
230      </p>
231<p>A lambda expression defines a C++ function object, hence function application syntax is like calling any other function object, for instance: <code class="literal">(_1 + _2)(i, j)</code>.
232
233
234      </p>
235<div class="section" lang="en">
236<div class="titlepage"><div><div><h5 class="title">
237<a name="lambda.partial_function_application"></a>Partial function application</h5></div></div></div>
238<p>
239A bind expression is in effect a <span class="emphasis"><em>partial function application</em></span>.
240In partial function application, some of the arguments of a function are bound to fixed values.
241          The result is another function, with possibly fewer arguments.
242          When called with the unbound arguments, this new function invokes the original function with the merged argument list of bound and unbound arguments.
243        </p>
244</div>
245<div class="section" lang="en">
246<div class="titlepage"><div><div><h5 class="title">
247<a name="lambda.terminology"></a>Terminology</h5></div></div></div>
248<p>
249          A lambda expression defines a function. A C++ lambda expression concretely constructs a function object, <span class="emphasis"><em>a functor</em></span>, when evaluated. We use the name <span class="emphasis"><em>lambda functor</em></span> to refer to such a function object.
250          Hence, in the terminology adopted here, the result of evaluating a lambda expression is a lambda functor.
251        </p>
252</div>
253</div>
254</div>
255<table width="100%"><tr>
256<td align="left"></td>
257<td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td>
258</tr></table>
259<hr>
260<div class="spirit-nav">
261<a accesskey="p" href="getting_started.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../lambda.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="using_library.html"><img src="../images/next.png" alt="Next"></a>
262</div>
263</body>
264</html>
Note: See TracBrowser for help on using the repository browser.