Browse Source

Adding fold to functional library

Samuel Jaffe 9 years ago
parent
commit
c99f4dd803

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.d

+ 7 - 5
stream.xcodeproj/project.pbxproj

@@ -16,6 +16,7 @@
 		0EB833521BBF45FD00DDC844 /* streams.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = streams.hpp; sourceTree = "<group>"; };
 		0EB833531BBF45FD00DDC844 /* streams.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = streams.cpp; sourceTree = "<group>"; };
 		0EB833541BBF45FD00DDC844 /* streams.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = streams.hpp; sourceTree = "<group>"; };
+		CD35DCF21D61385F00BE3686 /* fold.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = fold.hpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXGroup section */
@@ -33,6 +34,7 @@
 			isa = PBXGroup;
 			children = (
 				0EB8334D1BBF45FD00DDC844 /* filter.hpp */,
+				CD35DCF21D61385F00BE3686 /* fold.hpp */,
 				0EB8334E1BBF45FD00DDC844 /* forward.hpp */,
 				0EB8334F1BBF45FD00DDC844 /* join.hpp */,
 				0EB833501BBF45FD00DDC844 /* map.hpp */,
@@ -52,7 +54,7 @@
 			buildPhases = (
 			);
 			buildToolPath = /usr/bin/make;
-			buildWorkingDirectory = "/Users/leumasjaffe/Documents/Programming/XTools Workspace/C:C++/misc/stream";
+			buildWorkingDirectory = /Users/samjaffe/Documents/Programming/XTools/misc/stream;
 			dependencies = (
 			);
 			name = stream;
@@ -65,6 +67,7 @@
 		0E5DFDBA1BB4D3190063976E /* Project object */ = {
 			isa = PBXProject;
 			attributes = {
+				LastUpgradeCheck = 0720;
 			};
 			buildConfigurationList = 0E5DFDBD1BB4D3190063976E /* Build configuration list for PBXProject "stream" */;
 			compatibilityVersion = "Xcode 3.2";
@@ -86,23 +89,22 @@
 		0E5DFDBB1BB4D3190063976E /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				ARCHS = "$(ARCHS_STANDARD_32_BIT)";
 				COPY_PHASE_STRIP = NO;
+				ENABLE_TESTABILITY = YES;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
 				ONLY_ACTIVE_ARCH = YES;
-				SDKROOT = macosx10.6;
+				SDKROOT = macosx;
 			};
 			name = Debug;
 		};
 		0E5DFDBC1BB4D3190063976E /* Release */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				ARCHS = "$(ARCHS_STANDARD_32_BIT)";
 				COPY_PHASE_STRIP = YES;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES;
 				GCC_WARN_UNUSED_VARIABLE = YES;
-				SDKROOT = macosx10.6;
+				SDKROOT = macosx;
 			};
 			name = Release;
 		};

+ 1 - 1
stream.xcodeproj/xcuserdata/samjaffe.xcuserdatad/xcschemes/xcschememanagement.plist

@@ -7,7 +7,7 @@
 		<key>stream.xcscheme</key>
 		<dict>
 			<key>orderHint</key>
-			<integer>7</integer>
+			<integer>4</integer>
 		</dict>
 	</dict>
 	<key>SuppressBuildableAutocreation</key>

+ 1 - 0
streams.cpp

@@ -24,6 +24,7 @@ namespace stream {
 #define map(t, x, expr) stream::make_map_t<t>([](t x) { return expr; })
 #define filter(t, x, expr) stream::make_filter_t<t>([](t x) { return expr; })
 #define flatmap(t, x, expr) stream::make_flatmap_t<t>([](t x) { return expr; })
+#define fold(t, x, y, expr, init) stream::make_fold_t<t>([](t x, t y) { return expr; })
 
 template <typename T>
 std::ostream& operator<<(std::ostream& os, std::vector<T> const& tmp) {

+ 2 - 2
streams/filter.hpp

@@ -4,7 +4,7 @@ namespace stream {
   template <typename T>
   struct filter_t {
     template <typename F>
-    filter_t(F&& f) : pred(f) {}
+    explicit filter_t(F&& f) : pred(f) {}
     std::function<bool(const T&)> pred;
   };
 }
@@ -33,7 +33,7 @@ namespace stream {
         ~iterator() {}
         T operator*() override { return mem_; }
         
-        super& operator++() {
+        super& operator++() override {
           ++impl_;
           advance();
           return *this;

+ 23 - 0
streams/fold.hpp

@@ -0,0 +1,23 @@
+//
+//  fold.hpp
+//  stream
+//
+//  Created by Sam Jaffe on 8/14/16.
+//
+
+#pragma once
+
+#include <numeric>
+
+namespace stream {
+  template <typename L, typename R>
+  struct fold_left_t {
+    L init;
+    std::function<L(const L&, const R&)> fold;
+  };
+}
+
+template <typename L, typename R, bool B>
+L operator >(stream::detail::stream_base<R, B> const &s, stream::fold_left_t<L, R> && f) {
+  return std::accumulate(s.begin(), s.end(), f.fold, f.init);
+}

+ 4 - 4
streams/join.hpp

@@ -13,7 +13,7 @@ namespace stream {
   template <typename T, typename R>
   struct flatmap_t {
     template <typename F>
-    flatmap_t(F&& f) : func(f) {}
+    explicit flatmap_t(F&& f) : func(f) {}
     std::function<R(const T&)> func;
   };
 }
@@ -41,7 +41,7 @@ namespace stream {
         ~iterator() {}
         T operator*() override { return *curr_; }
         
-        super& operator++() {
+        super& operator++() override {
           ++curr_;
           advance();
           return *this;
@@ -61,8 +61,8 @@ namespace stream {
         typename C::iterator curr_, end_;
       };
       
-      join_stream(stream_base<C, B> const& sb) : source_(sb) {}
-      join_stream(stream_base<C, B> && sb) : source_(std::forward<stream_base<C, B>>(sb)) { }
+      explicit join_stream(stream_base<C, B> const& sb) : source_(sb) {}
+      explicit join_stream(stream_base<C, B> && sb) : source_(std::forward<stream_base<C, B>>(sb)) { }
       ~join_stream() override {}
       
       ::stream::iterator<T> begin() override {

+ 1 - 1
streams/map.hpp

@@ -4,7 +4,7 @@ namespace stream {
   template <typename T, typename R>
   struct map_t {
     template <typename F>
-    map_t(F&& f) : func(f) {}
+    explicit map_t(F&& f) : func(f) {}
     std::function<R(const T&)> func;
   };
 }

+ 3 - 3
streams/source.hpp

@@ -9,7 +9,7 @@ namespace stream {
       class iterator : public iterator_impl<value_type> {
       public:
         typedef iterator_impl<value_type> super;
-        iterator(typename C::iterator it) : impl_(it) {}
+        explicit iterator(typename C::iterator it) : impl_(it) {}
         ~iterator() {}
         value_type operator*() override { return *impl_; }
         DELEGATE_ITERATOR_IMPL(impl_)
@@ -17,8 +17,8 @@ namespace stream {
         typename C::iterator impl_;
       };
       
-      source_stream(C const& cont) : source_(cont) {}
-      source_stream(C && cont) : source_(std::forward(cont)) {}
+      explicit source_stream(C const& cont) : source_(cont) {}
+      explicit source_stream(C && cont) : source_(std::forward(cont)) {}
       ~source_stream() override {}
       ::stream::iterator<value_type> begin() override { return new iterator{source_.begin()}; }
       ::stream::iterator<value_type> end() override { return new iterator{source_.end()}; }

+ 4 - 3
streams/streams.hpp

@@ -1,7 +1,8 @@
 #pragma once
 
-#include <memory>
 #include <algorithm>
+#include <memory>
+#include <numeric>
 
 #define EQ_MEM(x) x == dynamic_cast<iterator const&>(other).x
 
@@ -17,7 +18,7 @@ namespace stream {
   template <typename T>
   class iterator {
   public:
-    iterator(detail::iterator_impl<T>* impl) : impl_(impl) {}
+    explicit iterator(detail::iterator_impl<T>* impl) : impl_(impl) {}
     iterator(iterator const& other) : impl_(other.impl_->clone()) { }
     iterator(iterator&& other) : impl_(nullptr) { std::swap(impl_, other.impl_); }
     ~iterator() { if (impl_) delete impl_; }
@@ -64,7 +65,7 @@ namespace stream {
       
       using self = stream_base<T, Own>;
     public:
-      stream_base(stream_impl<T>* impl) : impl_(impl) {}
+      explicit stream_base(stream_impl<T>* impl) : impl_(impl) {}
       stream_base(stream_base&&other) : impl_(nullptr) { std::swap(impl_, other.impl_); }
       ~stream_base() { if (Own && impl_) delete impl_; }
       ::stream::iterator<T> begin() const { return impl_->begin(); }