functools.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include "cartesian_product.h"
  3. #include "concatenate.h"
  4. #include "enumerate.h"
  5. #include "filtering.h"
  6. #include "mapped.h"
  7. #include "zip.h"
  8. #include <util/generic/adaptor.h>
  9. #include <util/generic/xrange.h>
  10. #include <tuple>
  11. #include <algorithm>
  12. namespace NFuncTools {
  13. using ::Enumerate;
  14. using ::Reversed;
  15. using ::Zip;
  16. using ::Concatenate;
  17. using ::CartesianProduct;
  18. template <typename TValue>
  19. auto Range(TValue from, TValue to, TValue step) {
  20. return xrange(from, to, step);
  21. }
  22. template <typename TValue>
  23. auto Range(TValue from, TValue to) {
  24. return xrange(from, to);
  25. }
  26. template <typename TValue>
  27. auto Range(TValue to) {
  28. return xrange(to);
  29. }
  30. //! Usage: for (i32 x : Map([](i32 x) { return x * x; }, a)) {...}
  31. template <typename TMapper, typename TContainerOrRef>
  32. auto Map(TMapper&& mapper, TContainerOrRef&& container) {
  33. return ::MakeMappedRange(std::forward<TContainerOrRef>(container), std::forward<TMapper>(mapper));
  34. }
  35. //! Usage: for (auto i : Map<int>(floats)) {...}
  36. template <typename TMapResult, typename TContainerOrRef>
  37. auto Map(TContainerOrRef&& container) {
  38. return Map([](const auto& x) { return TMapResult(x); }, std::forward<TContainerOrRef>(container));
  39. }
  40. //! Usage: for (i32 x : Filter(predicate, container)) {...}
  41. template <typename TPredicate, typename TContainerOrRef>
  42. auto Filter(TPredicate&& predicate, TContainerOrRef&& container) {
  43. return ::MakeFilteringRange(std::forward<TContainerOrRef>(container), std::forward<TPredicate>(predicate));
  44. }
  45. }