Last active
August 15, 2019 10:58
-
-
Save zxcq544/636ef587bee3d03704ddf255701f78a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<Semiregular T> | |
struct maybe_view : view_interface<maybe_view<T>> { | |
maybe_view() = default; | |
maybe_view(T t) : data_(std::move(t)) { | |
} | |
T const *begin() const noexcept { | |
return data_ ? &*data_ : nullptr; | |
} | |
T const *end() const noexcept { | |
return data_ ? &*data_ + 1 : nullptr; | |
} | |
private: | |
optional<T> data_{}; | |
}; | |
inline constexpr auto for_each = | |
[]<Range R, | |
Iterator I = iterator_t<R>, | |
IndirectUnaryInvocable<I> Fun>(R&& r, Fun fun) | |
requires Range<indirect_result_t<Fun, I>> { | |
return std::forward<R>(r) | |
| view::transform(std::move(fun)) | |
| view::join; | |
}; | |
inline constexpr auto yield_if = | |
[]<Semiregular T>(bool b, T x) { | |
return b ? maybe_view{std::move(x)} | |
: maybe_view<T>{}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment