Skip to content

Instantly share code, notes, and snippets.

@nihil84
Created February 10, 2019 16:28
Show Gist options
  • Save nihil84/ea703e32e3cffa61a317d7bccbc5acb9 to your computer and use it in GitHub Desktop.
Save nihil84/ea703e32e3cffa61a317d7bccbc5acb9 to your computer and use it in GitHub Desktop.
The composer functor
template <class A, class ...Reducers>
class reducpp::composer {
public:
using ReducersTuple = std::tuple<std::decay_t<Reducers> ...>;
using CompositeState = std::tuple<typename reducpp::_impl::reducer_traits<Reducers>::state_t ...>;
composer(const Reducers& ...reducers)
: m_reducers(reducers...) { }
CompositeState operator() (const CompositeState& state, const A& action) {
return apply(state, action, std::index_sequence_for<Reducers...>{});
}
template <std::size_t ...Is>
CompositeState apply(const CompositeState& state, const A& action, std::index_sequence<Is...>) {
return { std::get<Is>(m_reducers)(std::get<Is>(state), action) ... };
}
private:
const ReducersTuple m_reducers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment