Browse Source

feat: add first(), boolean aggregators, keys()/values() mapping

Sam Jaffe 3 years ago
parent
commit
39b272f3e2
1 changed files with 22 additions and 0 deletions
  1. 22 0
      include/stream/streams/streams.hpp

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

@@ -1,6 +1,7 @@
 #pragma once
 
 #include <algorithm>
+#include <optional>
 #include <memory>
 #include <numeric>
 #include <vector>
@@ -125,6 +126,19 @@ namespace stream {
         std::copy(begin(), end(), std::inserter(coll, coll.end()));
         return coll;
       }
+      
+      std::optional<value_type> first() const {
+        return begin() != end() ? std::optional(*begin()) : std::nullopt;
+      }
+            
+      template <typename F>
+      bool none(F &&pred) const { return std::none_of(begin(), end(), pred); }
+
+      template <typename F>
+      bool all(F &&pred) const { return std::all_of(begin(), end(), pred); }
+
+      template <typename F>
+      bool any(F &&pred) const { return std::any_of(begin(), end(), pred); }
 
       template <typename F>
       value_type accumulate(F && fold, value_type const & accum) const {
@@ -144,6 +158,14 @@ namespace stream {
       template <typename F> stream_base<T> filter(F && func) const &;
       template <typename F>
       stream_base<traits::fmapped_t<T, F>> flatmap(F && func) const &;
+      
+      auto keys() const & {
+        return map([](auto &kv) -> decltype(auto) { return kv.first; });
+      }
+
+      auto values() const & {
+        return map([](auto &kv) -> decltype(auto) { return kv.second; });
+      }
 
       template <typename F>
       stream_base<traits::mapped_t<T, F>> map(F && func) &&;