It is also extensible, so by specializing fit::unpack_sequence it can unpack other sequences as well. This post won’t focus on the extensibility of fit::unpack, but rather what can be done with fit::unpack when combined with other components in Fit.

Basic algorithms

We can implement simple algorithms such as transform(also know as a functor) by using the fit::by adaptor. The adaptor applies a projection over each of the parameters of the function. For the transform algorithm, the function is just the construction of std::tuple with the projection being applied to each parameter(or element) of the tuple:

Filter

Well, filter needs a little more work as the Fit library doesn’t provide a filter_adaptor. We can implement it by using a combination of join and transform(also know as a monad). First, we transform it to a sequence holding one element if it matches the predicate, otherwise, it will return an empty sequence. Next, we do a join which will flatten all these sequences together into one sequence. The empty sequences fall off, leaving just the elements that match the predicate.

So first, lets look how to implement join. This is actually very easy, since fit::unpack can unpack multiple sequences. All the elements from each sequence will be called on the function in order:

So now we compose this with transform to implement filter. Now we can’t check the predicate using a runtime if, we actually need a static_if. C++ doesn’t provide one, but we can use fit::if_ with fit::conditional to check the predicate:

Zipping sequences

The zip function combines two sequences so they can be unpacked together. Now, the Fit library does provides fit::combine that combines each function with a corresponding parameter. It is essentially zipping them together. However, we first need to transform the first sequence to a sequence of functions: