Commit 3c047a7b authored by Tom Jackson's avatar Tom Jackson Committed by Facebook Github Bot

eachTryTo, takeWhile

Reviewed By: yfeldblum

Differential Revision: D7257019

fbshipit-source-id: 2fe54fe1d42bfe81b2f6f8e43ae0f7e738e93edd
parent 608edf73
...@@ -238,6 +238,15 @@ class To { ...@@ -238,6 +238,15 @@ class To {
} }
}; };
template <class Dest>
class TryTo {
public:
template <class Value>
Expected<Dest, ConversionCode> operator()(Value&& value) const {
return ::folly::tryTo<Dest>(std::forward<Value>(value));
}
};
// Specialization to allow String->StringPiece conversion // Specialization to allow String->StringPiece conversion
template <> template <>
class To<StringPiece> { class To<StringPiece> {
...@@ -643,11 +652,18 @@ Visit visit(Visitor visitor = Visitor()) { ...@@ -643,11 +652,18 @@ Visit visit(Visitor visitor = Visitor()) {
return Visit(std::move(visitor)); return Visit(std::move(visitor));
} }
template <class Predicate, class Until = detail::Until<Predicate>> template <class Predicate = Identity, class Until = detail::Until<Predicate>>
Until until(Predicate pred = Predicate()) { Until until(Predicate pred = Predicate()) {
return Until(std::move(pred)); return Until(std::move(pred));
} }
template <
class Predicate = Identity,
class TakeWhile = detail::Until<Negate<Predicate>>>
TakeWhile takeWhile(Predicate pred = Predicate()) {
return TakeWhile(Negate<Predicate>(std::move(pred)));
}
template < template <
class Selector = Identity, class Selector = Identity,
class Comparer = Less, class Comparer = Less,
...@@ -689,9 +705,15 @@ Cast eachAs() { ...@@ -689,9 +705,15 @@ Cast eachAs() {
} }
// call folly::to on each value // call folly::to on each value
template <class Dest, class To = detail::Map<To<Dest>>> template <class Dest, class EachTo = detail::Map<To<Dest>>>
To eachTo() { EachTo eachTo() {
return To(); return EachTo();
}
// call folly::tryTo on each value
template <class Dest, class EachTryTo = detail::Map<TryTo<Dest>>>
EachTryTo eachTryTo() {
return EachTryTo();
} }
template <class Value> template <class Value>
......
...@@ -1225,6 +1225,21 @@ TEST(Gen, Guard) { ...@@ -1225,6 +1225,21 @@ TEST(Gen, Guard) {
runtime_error); runtime_error);
} }
TEST(Gen, eachTryTo) {
using std::runtime_error;
EXPECT_EQ(4,
from({"1", "a", "3"})
| eachTryTo<int>()
| dereference
| sum);
EXPECT_EQ(1,
from({"1", "a", "3"})
| eachTryTo<int>()
| takeWhile()
| dereference
| sum);
}
TEST(Gen, Batch) { TEST(Gen, Batch) {
EXPECT_EQ((vector<vector<int>> { {1} }), EXPECT_EQ((vector<vector<int>> { {1} }),
seq(1, 1) | batch(5) | as<vector>()); seq(1, 1) | batch(5) | as<vector>());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment