Browse Source

Add an accumulate that returns nullopt on an empty stream

Sam Jaffe 2 years ago
parent
commit
41191a1d91
1 changed files with 7 additions and 0 deletions
  1. 7 0
      include/stream/streams/streams.hpp

+ 7 - 0
include/stream/streams/streams.hpp

@@ -144,6 +144,13 @@ namespace stream {
       value_type accumulate(F && fold, value_type const & accum) const {
         return std::accumulate(begin(), end(), accum, fold);
       }
+      
+      template <typename F>
+      std::optional<value_type> accumulate(F && fold) const {
+        if (empty()) { return std::nullopt; }
+        value_type first = *begin();
+        return std::accumulate(++begin(), end(), first, fold);
+      }
 
       value_type accumulate(value_type const & accum) const {
         return std::accumulate(begin(), end(), accum);