Преглед изворни кода

Add an accumulate that returns nullopt on an empty stream

Sam Jaffe пре 2 година
родитељ
комит
41191a1d91
1 измењених фајлова са 7 додато и 0 уклоњено
  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);