Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Iterate.hpp

    ree22919 r45a091b  
    208208        return group_iterate_t<Args...>( std::forward<Args>( args )... );
    209209}
     210
     211// -----------------------------------------------------------------------------
     212// Helper struct and function to support
     213// for ( val : lazy_map( container1, f ) ) {}
     214// syntax to have a for each that iterates a container,
     215// mapping each element by applying f.
     216
     217template< typename T, typename Func >
     218struct lambda_iterate_t {
     219        const T & ref;
     220        std::function<Func> f;
     221
     222        struct iterator {
     223                typedef decltype(begin(ref)) Iter;
     224                Iter it;
     225                std::function<Func> f;
     226                iterator( Iter it, std::function<Func> f ) : it(it), f(f) {}
     227                iterator & operator++() {
     228                        ++it; return *this;
     229                }
     230                bool operator!=( const iterator &other ) const { return it != other.it; }
     231                auto operator*() const -> decltype(f(*it)) { return f(*it); }
     232        };
     233
     234        lambda_iterate_t( const T & ref, std::function<Func> f ) : ref(ref), f(f) {}
     235
     236        auto begin() const -> decltype(iterator(std::begin(ref), f)) { return iterator(std::begin(ref), f); }
     237        auto end() const   -> decltype(iterator(std::end(ref), f)) { return iterator(std::end(ref), f); }
     238};
     239
     240template< typename... Args >
     241lambda_iterate_t<Args...> lazy_map( const Args &... args ) {
     242        return lambda_iterate_t<Args...>( args...);
     243}
     244
     245// Local Variables: //
     246// tab-width: 4 //
     247// mode: c++ //
     248// compile-command: "make install" //
     249// End: //
Note: See TracChangeset for help on using the changeset viewer.