1 | <html> |
---|
2 | <head> |
---|
3 | <!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc --> |
---|
4 | <title>Place holders</title> |
---|
5 | <link rel="stylesheet" href="theme/style.css" type="text/css"> |
---|
6 | <link rel="prev" href="lazy_functions.html"> |
---|
7 | <link rel="next" href="polymorphic_functions.html"> |
---|
8 | </head> |
---|
9 | <body> |
---|
10 | <table width="100%" height="48" border="0" background="theme/bkd2.gif" cellspacing="2"> |
---|
11 | <tr> |
---|
12 | <td width="10"> |
---|
13 | </td> |
---|
14 | <td width="85%"> |
---|
15 | <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Place holders</b></font> |
---|
16 | </td> |
---|
17 | <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0"></a></td> |
---|
18 | </tr> |
---|
19 | </table> |
---|
20 | <br> |
---|
21 | <table border="0"> |
---|
22 | <tr> |
---|
23 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
24 | <td width="30"><a href="lazy_functions.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
25 | <td width="20"><a href="polymorphic_functions.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
26 | </tr> |
---|
27 | </table> |
---|
28 | <p> |
---|
29 | So far, apart from the quick start appetizer, we presented some examples of lazy functions using the ? symbol to act as a placeholder for yet unsupplied arguments. While this is understandable and simple, it is not adequate when we are dealing with complex composition of functions in addition to binary infix, unary prefix and postfix operators.</p> |
---|
30 | <p> |
---|
31 | When an arbitrarily complex function composition has M-N = U unsupplied arguments, the ? symbol maps this onto the actual non- lazy function taking U arguments. For example:</p> |
---|
32 | <code><pre> |
---|
33 | <span class=identifier>f1</span><span class=special>(</span><span class=identifier>f2</span><span class=special>(?, </span><span class=number>2</span><span class=special>), </span><span class=identifier>f3</span><span class=special>(?, </span><span class=identifier>f4</span><span class=special>(?))) --> </span><span class=identifier>unnamed_f</span><span class=special>(</span><span class=identifier>a</span><span class=special>, </span><span class=identifier>b</span><span class=special>, </span><span class=identifier>c</span><span class=special>) |
---|
34 | </span></pre></code> |
---|
35 | <p> |
---|
36 | Since there is only 1 supplied argument (N) and we are expecting 4 arguments (M), hence U = 3.</p> |
---|
37 | <p> |
---|
38 | It might not be immediately apparent how mapping takes place. It can naively be read from left to right; the first ? in our example maps to a, the second to b, and the last ? maps to c. Yet for even more complex compositions possibly with operators added in the mix, this becomes rather confusing. Also, this is not so flexible: in many occassions, we want to map two or more unknown arguments to a single place-holder.</p> |
---|
39 | <p> |
---|
40 | To avoid confusion, rather than using the ? as a symbol for unsupplied arguments, we use a more meaningful and precise representation. This is realized by supplying a numeric representation of the actual argument position (1 to N) in the resulting (right hand) function. Here's our revised example using this scheme:</p> |
---|
41 | <code><pre> |
---|
42 | <span class=identifier>f1</span><span class=special>(</span><span class=identifier>f2</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=number>2</span><span class=special>), </span><span class=identifier>f3</span><span class=special>(</span><span class=identifier>arg2</span><span class=special>, </span><span class=identifier>f4</span><span class=special>(</span><span class=identifier>arg3</span><span class=special>))) --> </span><span class=identifier>unnamed_f</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg2</span><span class=special>, </span><span class=identifier>arg3</span><span class=special>) |
---|
43 | </span></pre></code> |
---|
44 | <p> |
---|
45 | Now, arg1, arg2 and arg3 are used as placeholders instead of ?. Take note that with this revised scheme, we can now map two or more unsupplied arguments to a single actual argument. Example:</p> |
---|
46 | <code><pre> |
---|
47 | <span class=identifier>f1</span><span class=special>(</span><span class=identifier>f2</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=number>2</span><span class=special>), </span><span class=identifier>f3</span><span class=special>(</span><span class=identifier>arg2</span><span class=special>, </span><span class=identifier>f4</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>))) --> </span><span class=identifier>unnamed_f</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg2</span><span class=special>) |
---|
48 | </span></pre></code> |
---|
49 | <p> |
---|
50 | Notice how we mapped the leftmost and the rightmost unnamed argument to arg1. Consequently, the resulting (right hand) function now expects only two arguments (arg1 and arg2) instead of three. Here are some interesting snippets where this might be useful:</p> |
---|
51 | <code><pre> |
---|
52 | <span class=identifier>plus</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg1</span><span class=special>) --> </span><span class=identifier>mult_2</span><span class=special>(</span><span class=identifier>x</span><span class=special>) |
---|
53 | </span><span class=identifier>mult</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg1</span><span class=special>) --> </span><span class=identifier>square</span><span class=special>(</span><span class=identifier>x</span><span class=special>) |
---|
54 | </span><span class=identifier>mult</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>mult</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg1</span><span class=special>)) --> </span><span class=identifier>cube</span><span class=special>(</span><span class=identifier>x</span><span class=special>) |
---|
55 | </span></pre></code> |
---|
56 | <a name="extra_arguments"></a><h2>Extra arguments</h2><p> |
---|
57 | In C and C++, a function can have extra arguments that are not at all used by the function body itself. For instance, call-back functions may provide much more information than is actually needed at once. These extra arguments are just ignored.</p> |
---|
58 | <p> |
---|
59 | Phoenix also allows extra arguments to be passed. For example, recall our original add function:</p> |
---|
60 | <code><pre> |
---|
61 | <span class=identifier>add</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg2</span><span class=special>) |
---|
62 | </span></pre></code> |
---|
63 | <p> |
---|
64 | We know now that partially evaluating this function results to a function that expects 2 arguments. However, the framework is a bit more lenient and allows the caller to supply more arguments than is actually required. Thus, our partially evaluated plus(arg1, arg2) function actually allows 2 *or more* arguments to be passed in. For instance, with:</p> |
---|
65 | <code><pre> |
---|
66 | <span class=identifier>add</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg2</span><span class=special>)(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>) |
---|
67 | |
---|
68 | </span><span class=identifier>the </span><span class=identifier>third </span><span class=identifier>argument </span><span class=literal>'3' </span><span class=identifier>is </span><span class=identifier>merely </span><span class=identifier>ignored</span><span class=special>. |
---|
69 | </span></pre></code> |
---|
70 | <p> |
---|
71 | Taking this further, in-between arguments may even be ignored. Example:</p> |
---|
72 | <code><pre> |
---|
73 | <span class=identifier>add</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>, </span><span class=identifier>arg5</span><span class=special>)(</span><span class=number>1</span><span class=special>, </span><span class=number>2</span><span class=special>, </span><span class=number>3</span><span class=special>, </span><span class=number>4</span><span class=special>, </span><span class=number>5</span><span class=special>) |
---|
74 | </span></pre></code> |
---|
75 | <p> |
---|
76 | Here, arguments 2, 3, and 4 are ignored. The function add just takes in the first argument (arg1) and the fifth argument (arg5). The result is of course six (6).</p> |
---|
77 | <table width="80%" border="0" align="center"> |
---|
78 | <tr> |
---|
79 | <td class="note_box"> |
---|
80 | <img src="theme/lens.gif"></img> <b>Strict Arity</b><br><br>There are a few reasons why enforcing strict arity is not desireable. A case in point is the callback function. Typical callback functions provide more information than is actually needed. Lambda functions are often used as callbacks. </td> |
---|
81 | </tr> |
---|
82 | </table> |
---|
83 | <table border="0"> |
---|
84 | <tr> |
---|
85 | <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td> |
---|
86 | <td width="30"><a href="lazy_functions.html"><img src="theme/l_arr.gif" border="0"></a></td> |
---|
87 | <td width="20"><a href="polymorphic_functions.html"><img src="theme/r_arr.gif" border="0"></a></td> |
---|
88 | </tr> |
---|
89 | </table> |
---|
90 | <br> |
---|
91 | <hr size="1"> |
---|
92 | <p class="copyright">Copyright © 2001-2002 Joel de Guzman<br> |
---|
93 | <br> |
---|
94 | <font size="2">Use, modification and distribution is subject to the Boost Software |
---|
95 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
96 | http://www.boost.org/LICENSE_1_0.txt) </font> </p> |
---|
97 | </body> |
---|
98 | </html> |
---|