| Home | Libraries | People | FAQ | More

## Algorithm

#### Lazy Evaluation

Unlike MPL, Fusion algorithms are lazy[12] and non sequence-type preserving [13]. This is by design. Runtime efficiency is given a high priority. Like MPL, and unlike STL, fusion algorithms are mostly functional in nature such that algorithms are non mutating (no side effects). However, due to the high cost of returning full sequences such as vectors and lists, Views are returned from Fusion algorithms instead. For example, the `transform` algorithm does not actually return a transformed version of the original sequence. `transform` returns a `transform_view`. This view holds a reference to the original sequence plus the transform function. Iteration over the `transform_view` will apply the transform function over the sequence elements on demand. This lazy evaluation scheme allows us to chain as many algorithms as we want without incurring a high runtime penalty.

#### Sequence Extension

The lazy evaluation scheme where Algorithms return Views also allows operations such as `push_back` to be totally generic. In Fusion, `push_back` is actually a generic algorithm that works on all sequences. Given an input sequence `s` and a value `x`, Fusion's `push_back` algorithm simply returns a `joint_view`: a view that holds a reference to the original sequence `s` and the value `x`. Functions that were once sequence specific and need to be implemented N times over N different sequences are now implemented only once. That is to say that Fusion sequences are cheaply extensible.

To regain the original sequence, Conversion functions are provided. You may use one of the Conversion functions to convert back to the original sequence type.

#### Header

    #include <boost/fusion/algorithm.hpp>
    #include <boost/fusion/include/algorithm.hpp>

[12] Except for some special cases such as `for_each` and `copy` which are inherently imperative algorithms.

[13] What does that mean? It means that when you operate on a sequence through a Fusion algorithm that returns a sequence, the sequence returned may not be of the same class as the original