[iter] Implement hb_reduce

This commit is contained in:
Ebrahim Byagowi 2019-03-31 12:41:58 +04:30 committed by Behdad Esfahbod
parent f3aca6aa26
commit e526414c75
2 changed files with 33 additions and 0 deletions

View File

@ -348,6 +348,34 @@ static const struct
{ return hb_filter_iter_factory_t<Pred, Proj> (p, f); } { return hb_filter_iter_factory_t<Pred, Proj> (p, f); }
} hb_filter HB_UNUSED; } hb_filter HB_UNUSED;
template <typename Redu, typename TValue>
struct hb_reduce_t
{
hb_reduce_t (Redu r, TValue init_value) : r (r), init_value (init_value) {}
template <typename Iter,
hb_enable_if (hb_is_iterator (Iter))>
TValue
operator () (Iter it) const
{
TValue value = init_value;
for (; it; ++it)
value = r (*it, value);
return value;
}
private:
Redu r;
TValue init_value;
};
static const struct
{
template <typename Redu, typename TValue> hb_reduce_t<Redu, TValue>
operator () (Redu&& r, TValue init_value) const
{ return hb_reduce_t<Redu, TValue> (r, init_value); }
} hb_reduce HB_UNUSED;
/* hb_zip() */ /* hb_zip() */
template <typename A, typename B> template <typename A, typename B>

View File

@ -154,6 +154,11 @@ main (int argc, char **argv)
| hb_apply (&st) | hb_apply (&st)
; ;
+ hb_iter (src)
| hb_map ([&] (int i) -> int { return 1; })
| hb_reduce ([&] (int acc, int cur) -> int { return acc + cur; }, 2)
;
+ hb_iter (src) + hb_iter (src)
| hb_drain | hb_drain
; ;