Skip to content

Instantly share code, notes, and snippets.

@zxcq544
Last active August 15, 2019 10:58
Show Gist options
  • Save zxcq544/636ef587bee3d03704ddf255701f78a1 to your computer and use it in GitHub Desktop.
Save zxcq544/636ef587bee3d03704ddf255701f78a1 to your computer and use it in GitHub Desktop.
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