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.68.1"> |
---|
7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
---|
8 | <link rel="up" href="../lambda.html" title="Chapter 8. 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 C++ Libraries" 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><h2 class="title" style="clear: both"> |
---|
27 | <a name="id1243282"></a>Introduction</h2></div></div></div> |
---|
28 | <div class="toc"><dl> |
---|
29 | <dt><span class="section"><a href="s03.html#id1243287">Motivation</a></span></dt> |
---|
30 | <dt><span class="section"><a href="s03.html#id1243548">Introduction to lambda expressions</a></span></dt> |
---|
31 | </dl></div> |
---|
32 | <div class="section" lang="en"> |
---|
33 | <div class="titlepage"><div><div><h3 class="title"> |
---|
34 | <a name="id1243287"></a>Motivation</h3></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. |
---|
37 | Typically 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> |
---|
40 | Any C++ construct that can be called with the function call syntax |
---|
41 | is a function object. |
---|
42 | The 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>). |
---|
43 | As an example, one possible implementation for the standard <code class="literal">plus</code> template is: |
---|
44 | |
---|
45 | </p> |
---|
46 | <pre class="programlisting"> |
---|
47 | template <class T> |
---|
48 | struct plus : public binary_function<T, T, T> { |
---|
49 | T operator()(const T& i, const T& j) const { |
---|
50 | return i + j; |
---|
51 | } |
---|
52 | }; |
---|
53 | </pre> |
---|
54 | <p> |
---|
55 | |
---|
56 | The base class <code class="literal">binary_function<T, T, T></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> |
---|
59 | In addition to the basic function object classes, such as the one above, |
---|
60 | the 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. |
---|
61 | For example, instead of having to explicitly write a function object class like: |
---|
62 | |
---|
63 | </p> |
---|
64 | <pre class="programlisting"> |
---|
65 | class plus_1 { |
---|
66 | int _i; |
---|
67 | public: |
---|
68 | plus_1(const int& i) : _i(i) {} |
---|
69 | int operator()(const int& j) { return _i + j; } |
---|
70 | }; |
---|
71 | </pre> |
---|
72 | <p> |
---|
73 | |
---|
74 | the equivalent functionality can be achieved with the <code class="literal">plus</code> template and one of the binder templates (<code class="literal">bind1st</code>). |
---|
75 | E.g., the following two expressions create function objects with identical functionalities; |
---|
76 | when 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"> |
---|
80 | plus_1(1) |
---|
81 | bind1st(plus<int>(), 1) |
---|
82 | </pre> |
---|
83 | <p> |
---|
84 | |
---|
85 | The subexpression <code class="literal">plus<int>()</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>. |
---|
86 | As 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"> |
---|
90 | transform(a.begin(), a.end(), ostream_iterator<int>(cout), |
---|
91 | bind1st(plus<int>(), 1)); |
---|
92 | </pre> |
---|
93 | <p> |
---|
94 | |
---|
95 | </p> |
---|
96 | <p> |
---|
97 | To make the binder templates more generally applicable, the STL contains <span class="emphasis"><em>adaptors</em></span> for making |
---|
98 | pointers or references to functions, and pointers to member functions, |
---|
99 | adaptable. |
---|
100 | |
---|
101 | Finally, some STL implementations contain function composition operations as |
---|
102 | extensions to the standard [<a href="../lambda.html#cit:sgi:02" title="[SGI02]"><span class="abbrev">SGI02</span></a>]. |
---|
103 | </p> |
---|
104 | <p> |
---|
105 | All these tools aim at one goal: to make it possible to specify |
---|
106 | <span class="emphasis"><em>unnamed functions</em></span> in a call of an STL algorithm, |
---|
107 | in other words, to pass code fragments as an argument to a function. |
---|
108 | |
---|
109 | However, this goal is attained only partially. |
---|
110 | The simple example above shows that the definition of unnamed functions |
---|
111 | with the standard tools is cumbersome. |
---|
112 | |
---|
113 | Complex expressions involving functors, adaptors, binders and |
---|
114 | function composition operations tend to be difficult to comprehend. |
---|
115 | |
---|
116 | In addition to this, there are significant restrictions in applying |
---|
117 | the standard tools. E.g. the standard binders allow only one argument |
---|
118 | of a binary function to be bound; there are no binders for |
---|
119 | 3-ary, 4-ary etc. functions. |
---|
120 | </p> |
---|
121 | <p> |
---|
122 | The Boost Lambda Library provides solutions for the problems described above: |
---|
123 | |
---|
124 | </p> |
---|
125 | <div class="itemizedlist"><ul type="disc"> |
---|
126 | <li> |
---|
127 | <p> |
---|
128 | Unnamed functions can be created easily with an intuitive syntax. |
---|
129 | |
---|
130 | The above example can be written as: |
---|
131 | |
---|
132 | </p> |
---|
133 | <pre class="programlisting"> |
---|
134 | transform(a.begin(), a.end(), ostream_iterator<int>(cout), |
---|
135 | 1 + _1); |
---|
136 | </pre> |
---|
137 | <p> |
---|
138 | |
---|
139 | or even more intuitively: |
---|
140 | |
---|
141 | </p> |
---|
142 | <pre class="programlisting"> |
---|
143 | for_each(a.begin(), a.end(), cout << (1 + _1)); |
---|
144 | </pre> |
---|
145 | <p> |
---|
146 | </p> |
---|
147 | </li> |
---|
148 | <li><p> |
---|
149 | Most of the restrictions in argument binding are removed, |
---|
150 | arbitrary arguments of practically any C++ function can be bound. |
---|
151 | </p></li> |
---|
152 | <li><p> |
---|
153 | Separate function composition operations are not needed, |
---|
154 | as function composition is supported implicitly. |
---|
155 | |
---|
156 | </p></li> |
---|
157 | </ul></div> |
---|
158 | <p> |
---|
159 | |
---|
160 | </p> |
---|
161 | </div> |
---|
162 | <div class="section" lang="en"> |
---|
163 | <div class="titlepage"><div><div><h3 class="title"> |
---|
164 | <a name="id1243548"></a>Introduction to lambda expressions</h3></div></div></div> |
---|
165 | <div class="toc"><dl> |
---|
166 | <dt><span class="section"><a href="s03.html#lambda.partial_function_application">Partial function application</a></span></dt> |
---|
167 | <dt><span class="section"><a href="s03.html#lambda.terminology">Terminology</a></span></dt> |
---|
168 | </dl></div> |
---|
169 | <p> |
---|
170 | Lambda expression are common in functional programming languages. |
---|
171 | Their syntax varies between languages (and between different forms of lambda calculus), but the basic form of a lambda expressions is: |
---|
172 | |
---|
173 | |
---|
174 | </p> |
---|
175 | <pre class="programlisting"> |
---|
176 | lambda x<sub>1</sub> ... x<sub>n</sub>.e |
---|
177 | </pre> |
---|
178 | <p> |
---|
179 | |
---|
180 | |
---|
181 | A lambda expression defines an unnamed function and consists of: |
---|
182 | </p> |
---|
183 | <div class="itemizedlist"><ul type="disc"> |
---|
184 | <li><p> |
---|
185 | the parameters of this function: <code class="literal">x<sub>1</sub> ... x<sub>n</sub></code>. |
---|
186 | |
---|
187 | </p></li> |
---|
188 | <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>. |
---|
189 | </p></li> |
---|
190 | </ul></div> |
---|
191 | <p> |
---|
192 | |
---|
193 | A simple example of a lambda expression is |
---|
194 | </p> |
---|
195 | <pre class="programlisting"> |
---|
196 | lambda x y.x+y |
---|
197 | </pre> |
---|
198 | <p> |
---|
199 | Applying the lambda function means substituting the formal parameters with the actual arguments: |
---|
200 | </p> |
---|
201 | <pre class="programlisting"> |
---|
202 | (lambda x y.x+y) 2 3 = 2 + 3 = 5 |
---|
203 | </pre> |
---|
204 | <p> |
---|
205 | |
---|
206 | |
---|
207 | </p> |
---|
208 | <p> |
---|
209 | In 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. |
---|
210 | In the current version of the library, |
---|
211 | there are three such predefined formal parameters, |
---|
212 | called <span class="emphasis"><em>placeholders</em></span>: |
---|
213 | <code class="literal">_1</code>, <code class="literal">_2</code> and <code class="literal">_3</code>. |
---|
214 | They refer to the first, second and third argument of the function defined |
---|
215 | by the lambda expression. |
---|
216 | |
---|
217 | For example, the C++ version of the definition |
---|
218 | </p> |
---|
219 | <pre class="programlisting">lambda x y.x+y</pre> |
---|
220 | <p> |
---|
221 | is |
---|
222 | </p> |
---|
223 | <pre class="programlisting">_1 + _2</pre> |
---|
224 | <p> |
---|
225 | </p> |
---|
226 | <p> |
---|
227 | Hence, there is no syntactic keyword for C++ lambda expressions. |
---|
228 | The use of a placeholder as an operand implies that the operator invocation is a lambda expression. |
---|
229 | However, this is true only for operator invocations. |
---|
230 | Lambda expressions containing function calls, control structures, casts etc. require special syntactic constructs. |
---|
231 | Most importantly, function calls need to be wrapped inside a <code class="literal">bind</code> function. |
---|
232 | |
---|
233 | As an example, consider the lambda expression: |
---|
234 | |
---|
235 | </p> |
---|
236 | <pre class="programlisting">lambda x y.foo(x,y)</pre> |
---|
237 | <p> |
---|
238 | |
---|
239 | Rather than <code class="literal">foo(_1, _2)</code>, the C++ counterpart for this expression is: |
---|
240 | |
---|
241 | </p> |
---|
242 | <pre class="programlisting">bind(foo, _1, _2)</pre> |
---|
243 | <p> |
---|
244 | |
---|
245 | We refer to this type of C++ lambda expressions as <span class="emphasis"><em>bind expressions</em></span>. |
---|
246 | </p> |
---|
247 | <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>. |
---|
248 | |
---|
249 | |
---|
250 | </p> |
---|
251 | <div class="section" lang="en"> |
---|
252 | <div class="titlepage"><div><div><h4 class="title"> |
---|
253 | <a name="lambda.partial_function_application"></a>Partial function application</h4></div></div></div> |
---|
254 | <p> |
---|
255 | A bind expression is in effect a <span class="emphasis"><em>partial function application</em></span>. |
---|
256 | In partial function application, some of the arguments of a function are bound to fixed values. |
---|
257 | The result is another function, with possibly fewer arguments. |
---|
258 | When called with the unbound arguments, this new function invokes the original function with the merged argument list of bound and unbound arguments. |
---|
259 | </p> |
---|
260 | </div> |
---|
261 | <div class="section" lang="en"> |
---|
262 | <div class="titlepage"><div><div><h4 class="title"> |
---|
263 | <a name="lambda.terminology"></a>Terminology</h4></div></div></div> |
---|
264 | <p> |
---|
265 | 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. |
---|
266 | Hence, in the terminology adopted here, the result of evaluating a lambda expression is a lambda functor. |
---|
267 | </p> |
---|
268 | </div> |
---|
269 | </div> |
---|
270 | </div> |
---|
271 | <table width="100%"><tr> |
---|
272 | <td align="left"></td> |
---|
273 | <td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td> |
---|
274 | </tr></table> |
---|
275 | <hr> |
---|
276 | <div class="spirit-nav"> |
---|
277 | <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> |
---|
278 | </div> |
---|
279 | </body> |
---|
280 | </html> |
---|