Skip to content

Commit 4a1ada4

Browse files
committed
Merge branch 'master' of github.com:LoopPerfect/conduit
2 parents 0ed1f6a + f91154f commit 4a1ada4

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

include/conduit/compose.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ auto compose(F f, Fs... fs) {
2525
namespace operators {
2626
template <class Xs, class F>
2727
auto operator >> (Xs&& xs, F&& f)
28-
-> decltype(xs.begin(), f(FWD(xs)).begin(), f(FWD(xs))) {
28+
-> decltype(xs.begin(), f(FWD(xs))) {
2929
return f(FWD(xs));
3030
}
3131

include/conduit/sum.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef CONDUIT_SUM_HPP
2+
#define CONDUIT_SUM_HPP
3+
4+
#include <conduit/meta.hpp>
5+
#include <conduit/seq.hpp>
6+
7+
namespace conduit {
8+
9+
static constexpr auto sum() {
10+
return [=](auto&& xs) {
11+
using T = decltype(first(xs));
12+
T result = T{};
13+
for (auto&&x: xs) {
14+
result+=x;
15+
}
16+
return result;
17+
};
18+
};
19+
20+
} // namespace conduit
21+
22+
#endif

include/conduit/to-vector.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef CONDUIT_TO_VECTOR_HPP
2+
#define CONDUIT_TO_VECTOR_HPP
3+
4+
#include <vector>
5+
6+
namespace conduit {
7+
auto toVector = [](std::size_t n=0) {
8+
return [n](auto&& xs) -> std::vector<decltype(first(xs))> {
9+
using T = decltype(first(xs));
10+
auto vec = std::vector<T>();
11+
vec.reserve(n);
12+
for (auto&&x: xs) {
13+
vec.emplace_back(std::move(x));
14+
}
15+
return vec;
16+
};
17+
};
18+
}
19+
20+
#endif

test/sum.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <gtest/gtest.h>
2+
#include <conduit/sum.hpp>
3+
#include <conduit/range.hpp>
4+
#include <conduit/compose.hpp>
5+
#include <conduit/map.hpp>
6+
7+
#include <conduit/allocators/terminate.hpp>
8+
9+
using namespace conduit;
10+
using namespace operators;
11+
12+
TEST(Seq, sum_some) {
13+
auto value = range(0, 4)
14+
>> sum();
15+
16+
EXPECT_EQ(value, 6);
17+
}
18+
19+
20+
TEST(Seq, sum_none) {
21+
auto value = range(0, 0)
22+
>> sum();
23+
24+
EXPECT_EQ(value, 0);
25+
}
26+
27+
28+
TEST(Seq, sum_floats) {
29+
auto value = range(0, 4)
30+
>> map([](auto x) { return (float)x; })
31+
>> sum();
32+
33+
EXPECT_EQ(value, 6.0f);
34+
}

test/to-vector.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <gtest/gtest.h>
2+
#include <conduit/to-vector.hpp>
3+
#include <conduit/range.hpp>
4+
#include <conduit/filter.hpp>
5+
#include <conduit/take.hpp>
6+
#include <conduit/compose.hpp>
7+
8+
using namespace conduit;
9+
using namespace operators;
10+
11+
TEST(Seq, toVector) {
12+
13+
auto items = range()
14+
>> filter([](auto x){ return x%2==0; })
15+
>> take(4)
16+
>> toVector();
17+
18+
int i = 0;
19+
for (auto x : items) {
20+
EXPECT_EQ(i*2, x);
21+
++i;
22+
}
23+
24+
EXPECT_EQ(i, 4);
25+
}
26+
27+

0 commit comments

Comments
 (0)