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 {
}
};
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
template <>
class To<StringPiece> {
......@@ -643,11 +652,18 @@ Visit visit(Visitor visitor = 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()) {
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 <
class Selector = Identity,
class Comparer = Less,
......@@ -689,9 +705,15 @@ Cast eachAs() {
}
// call folly::to on each value
template <class Dest, class To = detail::Map<To<Dest>>>
To eachTo() {
return To();
template <class Dest, class EachTo = detail::Map<To<Dest>>>
EachTo eachTo() {
return EachTo();
}
// call folly::tryTo on each value
template <class Dest, class EachTryTo = detail::Map<TryTo<Dest>>>
EachTryTo eachTryTo() {
return EachTryTo();
}
template <class Value>
......
......@@ -1225,6 +1225,21 @@ TEST(Gen, Guard) {
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) {
EXPECT_EQ((vector<vector<int>> { {1} }),
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