ryanhaining / cppitertools

Implementation of python itertools and builtin iteration functions for C++17
https://twitter.com/cppitertools
BSD 2-Clause "Simplified" License
1.37k stars 115 forks source link

repeat in product #36

Closed snirgaz closed 7 years ago

snirgaz commented 7 years ago

Would it be possible to add support for "repeat" in the product function similar to python? (https://docs.python.org/2/library/itertools.html#itertools.product) e.g -- product<4>(A,(4?)) would iterate over the cartesian product of 4 copies of A.

Thanks!

ryanhaining commented 7 years ago

Interesting. product<4>(A); seems easy enough though lacking, I'm not sure about product(A, 4);

ryanhaining commented 7 years ago

for product<4>(A) this seems to do the job. but a runtime argument would be better... trying to write that without having to do a new product class:

namespace iter {
  namespace impl {
    template <std::size_t... Is, typename Container>
    decltype(auto) product_repeat_impl(
        std::index_sequence<Is...>, Container&& container) {
      return product(((void)Is, Container(container))...);
    }
  }
  template <std::size_t N, typename Container>
  decltype(auto) product(Container&& container) {
    return impl::product_repeat_impl(
        std::make_index_sequence<N>{}, std::forward<Container>(container));
  }
}
snirgaz commented 7 years ago

For my needs, I am fine with a compile time version. Are you planning to incorporate this is the code? Thanks!

ryanhaining commented 7 years ago

done. I guess there's nothing stopping me from adding a version with an integral argument later