• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[section The YAP Way]
2
3There are certain idioms that _yap_ is written to support.  Before getting
4into the nuts and bolts of how _yap_ operates, let's define these idioms.
5
6[heading _eval_xform_]
7
8This is the main idiom you'll see reinforced in the examples.  The idea is
9that you capture an expression:
10
11    auto expr_0 = /* ... */ ;
12
13then transform it one or more times:
14
15    auto expr_1 = boost::yap::transform(expr_0, my_transform_1);
16    auto expr_2 = boost::yap::transform(expr_1, my_transform_2);
17    // ...
18    auto expr_n = boost::yap::transform(expr_n_minus_1, my_transform_n);
19
20and then finally you evaluate it:
21
22    auto const result = boost::yap::evaluate(expr_n);
23
24Each call to _xform_ here produces a new _Expr_ that can subsequently be
25transformed.  This is conceptually similar to what happens inside many
26compilers.  Capturing the expression is analogous to the compiler's parse; the
27transformations are analogous to optimization passes; and the evaluation is
28analogous to code generation.
29
30This keeps the meaning of your code quite clear and easy to follow.  For this
31reason, I think you should try to use _yap_ in this way when you can.
32
33[heading _xform_as_eval_]
34
35This is a variant of _eval_xform_, where the _eval_ call at the end is
36unnecessary, because the final (or perhaps only) transform does all the
37evaluation we need.
38
39For instance, here is the `get_arity` transform object used in the _calc3_
40example (don't worry too much about the implementation _emdash_ we'll return
41to this later in the docs in much greater detail):
42
43[calc3_get_arity_xform]
44
45Here is how this might be used:
46
47    auto expr = 1_p * 2_p;
48    auto const arity = boost::yap::transform(expr, get_arity{});
49    static_assert(arity.value == 2, "Called with wrong number of args.");
50
51In this case, _xform_ produces a non-_Expr_ value, all by itself.  We got our
52result without ever needing to call _eval_.
53
54[note Whether _xform_ returns an _Expr_ or non-_Expr_ is entirely up to the
55caller.  The transform object passed as the second argument to _xform_ defines
56what _xform_'s return type will be.]
57
58[endsect]
59