| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- //
- // accumulate.h
- // stream
- //
- // Created by Sam Jaffe on 3/29/23.
- //
- #pragma once
- #include <optional>
- #include <stream/forward.h>
- #include <stream/detail/identity.h>
- #include <stream/detail/traits.h>
- #include <stream/detail/macro.h>
- namespace stream::ranges {
- template <typename It, typename S, typename T, typename F>
- auto fold_left(It it, S end, T init, F reduce) {
- for (; it != end; ++it) {
- init = std::invoke(reduce, init, *it);
- }
- return init;
- }
- template <typename Stream, typename T, typename F>
- auto fold_left(Stream && stream, T init, F reduce) {
- for (auto && elem : FWD(stream)) {
- init = std::invoke(reduce, init, FWD(elem));
- }
- return init;
- }
- template <typename Stream, typename F>
- auto fold_left_with_first(Stream && stream, F reduce) {
- return fold_left(++stream.begin(), stream.end(), *stream.begin(),
- std::ref(reduce));
- }
- }
- #include <stream/detail/undef.h>
|