Skip to content

Commit

Permalink
Merge branch 'cpp14' into cpp14tomaster
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaining committed Mar 28, 2016
2 parents a9c719a + 2281de8 commit 1f3ec2e
Show file tree
Hide file tree
Showing 31 changed files with 810 additions and 662 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CPPItertools
CPPItertools C++14 development Branch
============

**NOTE**: this branch is for refining and moving forward with the C++14
standard. It will be merged into master when compiler and library
support for the standard approaches completion in common compilers.
Specifically I'm considering clang and gcc, along with libstdc++
and libc++

Range-based for loop add-ons inspired by the Python builtins and itertools
library. Like itertools and the Python3 builtins, this library uses lazy
evaluation wherever possible.
Expand All @@ -23,6 +29,7 @@ evaluation wherever possible.
[repeat](#repeat)<br />
[count](#count)<br />
[groupby](#groupby)<br />
[starmap](#starmap)<br />
[accumulate](#accumulate)<br />
[compress](#compress)<br />
[sorted](#sorted)<br />
Expand Down Expand Up @@ -375,6 +382,48 @@ for (auto&& gb : groupby(vec, [] (const string &s) {return s.length(); })) {
It just iterates through, making a new group each time there is a key change.
Thus, if the group is unsorted, the same key may appear multiple times.

starmap
-------

Takes a sequence of tuple-like objects (anything that works with `std::get`)
and unpacks each object into individual arguments for each function call.
The below example takes a `vector` of `pairs` of ints, and passes them
to a function expecting two ints, with the elements of the `pair` being
the first and second arguments to the function.

```c++
vector<pair<int, int>> v = {{2, 3}, {5, 2}, {3, 4}}; // {base, exponent}
for (auto&& i : starmap([](int b, int e){return pow(b, e);}, v)) {
// ...
}
```
`starmap` can also work over a tuple-like object of tuple-like objects even
when the contained objects are different as long as the functor works with
multiple types of calls. For example, a `Callable` struct with overloads
for its `operator()` will work as long as all overloads have the same
return type
```c++
struct Callable {
int operator()(int i) const;
int operator()(int i, char c) const;
int operator()(double d, int i, char c) const;
};
```

This will work with a tuple of mixed types

```c++
auto t = make_tuple(
make_tuple(5), // first form
make_pair(3, 'c'), // second
make_tuple(1.0, 1, '1')); // third
for (auto&& i : starmap(Callable{}, t)) {
// ...
}
```
accumulate
-------
*Additional Requirements*: Type return from functor (with reference removed)
Expand Down
15 changes: 5 additions & 10 deletions accumulate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ class iter::impl::Accumulator {
friend Accumulator<std::initializer_list<T>, AF> iter::accumulate(
std::initializer_list<T>, AF);

// AccumVal must be default constructible
using AccumVal =
typename std::remove_reference<typename std::result_of<AccumulateFunc(
iterator_deref<Container>, iterator_deref<Container>)>::type>::type;
using AccumVal = std::remove_reference_t<std::result_of_t<AccumulateFunc(
iterator_deref<Container>, iterator_deref<Container>)>>;

Accumulator(Container&& in_container, AccumulateFunc in_accumulate_func)
: container(std::forward<Container>(in_container)),
Expand Down Expand Up @@ -143,12 +141,9 @@ namespace iter {
template <typename Container>
auto accumulate(Container&& container) -> decltype(accumulate(
std::forward<Container>(container),
std::plus<typename std::
remove_reference<impl::iterator_deref<Container>>::type>{})) {
return accumulate(
std::forward<Container>(container),
std::plus<typename std::
remove_reference<impl::iterator_deref<Container>>::type>{});
std::plus<std::remove_reference_t<impl::iterator_deref<Container>>>{})) {
return accumulate(std::forward<Container>(container),
std::plus<std::remove_reference_t<impl::iterator_deref<Container>>>{});
}

template <typename T>
Expand Down
Loading

0 comments on commit 1f3ec2e

Please sign in to comment.