You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2017/08/01 21:03:05 UTC

[07/16] mesos git commit: Added a functional "map" abstraction.

Added a functional "map" abstraction.

Review: https://reviews.apache.org/r/61054


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/b28548f1
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/b28548f1
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/b28548f1

Branch: refs/heads/master
Commit: b28548f14d356d274ce9f6ba802a5beae7723ba6
Parents: 90cc4f1
Author: Benjamin Hindman <be...@gmail.com>
Authored: Thu Jul 20 23:36:29 2017 -0700
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Tue Aug 1 14:01:51 2017 -0700

----------------------------------------------------------------------
 3rdparty/stout/Makefile.am              |   1 +
 3rdparty/stout/include/stout/lambda.hpp | 124 +++++++++++++++++++++++++++
 3rdparty/stout/tests/CMakeLists.txt     |   1 +
 3rdparty/stout/tests/lambda_tests.cpp   |  90 +++++++++++++++++++
 4 files changed, 216 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/b28548f1/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/stout/Makefile.am b/3rdparty/stout/Makefile.am
index 20c3c1b..3ecafee 100644
--- a/3rdparty/stout/Makefile.am
+++ b/3rdparty/stout/Makefile.am
@@ -143,6 +143,7 @@ stout_tests_SOURCES =			\
   tests/ip_tests.cpp			\
   tests/json_tests.cpp			\
   tests/jsonify_tests.cpp		\
+  tests/lambda_tests.cpp		\
   tests/linkedhashmap_tests.cpp		\
   tests/mac_tests.cpp			\
   tests/main.cpp			\

http://git-wip-us.apache.org/repos/asf/mesos/blob/b28548f1/3rdparty/stout/include/stout/lambda.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/lambda.hpp b/3rdparty/stout/include/stout/lambda.hpp
index e67acf5..5d7cd29 100644
--- a/3rdparty/stout/include/stout/lambda.hpp
+++ b/3rdparty/stout/include/stout/lambda.hpp
@@ -13,7 +13,9 @@
 #ifndef __STOUT_LAMBDA_HPP__
 #define __STOUT_LAMBDA_HPP__
 
+#include <algorithm>
 #include <functional>
+#include <vector>
 
 namespace lambda {
 
@@ -24,6 +26,128 @@ using std::ref;
 
 using namespace std::placeholders;
 
+
+template <
+  template <typename...> class Iterable,
+  typename F,
+  typename U,
+  typename V = typename std::result_of<F(U)>::type>
+Iterable<V> map(F&& f, const Iterable<U>& input)
+{
+  Iterable<V> output;
+  std::transform(
+      input.begin(),
+      input.end(),
+      std::inserter(output, output.begin()),
+      std::forward<F>(f));
+  return output;
+}
+
+
+template <
+  template <typename...> class OutputIterable,
+  template <typename...> class InputIterable,
+  typename F,
+  typename U,
+  typename V = typename std::result_of<F(U)>::type>
+OutputIterable<V> map(F&& f, const InputIterable<U>& input)
+{
+  OutputIterable<V> output;
+  std::transform(
+      input.begin(),
+      input.end(),
+      std::inserter(output, output.begin()),
+      std::forward<F>(f));
+  return output;
+}
+
+
+template <
+  template <typename...> class Iterable,
+  typename F,
+  typename U,
+  typename V = typename std::result_of<F(U)>::type,
+  typename = typename std::enable_if<
+    !std::is_same<U, V>::value>::type>
+Iterable<V> map(F&& f, Iterable<U>&& input)
+{
+  Iterable<V> output;
+  std::transform(
+      std::make_move_iterator(input.begin()),
+      std::make_move_iterator(input.end()),
+      std::inserter(output, output.begin()),
+      std::forward<F>(f));
+  return output;
+}
+
+
+template <
+  template <typename...> class Iterable,
+  typename F,
+  typename U,
+  typename = typename std::enable_if<
+    std::is_same<U, typename std::result_of<F(U)>::type>::value>::type>
+Iterable<U>&& map(F&& f, Iterable<U>&& iterable)
+{
+  std::transform(
+      std::make_move_iterator(iterable.begin()),
+      std::make_move_iterator(iterable.end()),
+      iterable.begin(),
+      std::forward<F>(f));
+  return std::move(iterable);
+}
+
+
+template <
+  template <typename...> class OutputIterable,
+  template <typename...> class InputIterable,
+  typename F,
+  typename U,
+  typename V = typename std::result_of<F(U)>::type>
+OutputIterable<V> map(F&& f, InputIterable<U>&& input)
+{
+  OutputIterable<V> output;
+  std::transform(
+      std::make_move_iterator(input.begin()),
+      std::make_move_iterator(input.end()),
+      std::inserter(output, output.begin()),
+      std::forward<F>(f));
+  return output;
+}
+
+
+template <
+  template <typename...> class OutputIterable,
+  typename F,
+  typename U,
+  typename V = typename std::result_of<F(U)>::type>
+OutputIterable<V> map(F&& f, std::initializer_list<U> input)
+{
+  OutputIterable<V> output;
+  std::transform(
+      input.begin(),
+      input.end(),
+      std::inserter(output, output.begin()),
+      std::forward<F>(f));
+  return output;
+}
+
+
+template <
+  typename F,
+  typename U,
+  typename V = typename std::result_of<F(U)>::type>
+std::vector<V> map(F&& f, std::initializer_list<U> input)
+{
+  std::vector<V> output;
+  std::transform(
+      input.begin(),
+      input.end(),
+      std::inserter(output, output.begin()),
+      std::forward<F>(f));
+  return output;
+}
+
 } // namespace lambda {
 
 #endif // __STOUT_LAMBDA_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/b28548f1/3rdparty/stout/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/CMakeLists.txt b/3rdparty/stout/tests/CMakeLists.txt
index 12882ee..953be64 100644
--- a/3rdparty/stout/tests/CMakeLists.txt
+++ b/3rdparty/stout/tests/CMakeLists.txt
@@ -33,6 +33,7 @@ set(STOUT_ROOT_TESTS_SRC
   ip_tests.cpp
   json_tests.cpp
   jsonify_tests.cpp
+  lambda_tests.cpp
   linkedhashmap_tests.cpp
   mac_tests.cpp
   main.cpp

http://git-wip-us.apache.org/repos/asf/mesos/blob/b28548f1/3rdparty/stout/tests/lambda_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/lambda_tests.cpp b/3rdparty/stout/tests/lambda_tests.cpp
new file mode 100644
index 0000000..ad8c2ef
--- /dev/null
+++ b/3rdparty/stout/tests/lambda_tests.cpp
@@ -0,0 +1,90 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License
+
+#include <gtest/gtest.h>
+
+#include <stout/lambda.hpp>
+#include <stout/numify.hpp>
+
+
+struct OnlyMoveable
+{
+  OnlyMoveable(int i) : i(i) {}
+  OnlyMoveable(OnlyMoveable&&) = default;
+  OnlyMoveable(const OnlyMoveable&) = delete;
+  OnlyMoveable& operator=(OnlyMoveable&&) = default;
+  OnlyMoveable& operator=(const OnlyMoveable&) = delete;
+
+  int i;
+  int j = 0;
+};
+
+
+std::vector<std::string> function()
+{
+  return {"1", "2", "3"};
+}
+
+
+TEST(LambdaTest, Map)
+{
+  std::vector<int> expected = {1, 2, 3};
+
+  EXPECT_EQ(
+      expected,
+      lambda::map(
+          [](std::string s) {
+            return numify<int>(s).get();
+          },
+          std::vector<std::string>{"1", "2", "3"}));
+
+  EXPECT_EQ(
+      expected,
+      lambda::map(
+          [](const std::string& s) {
+            return numify<int>(s).get();
+          },
+          std::vector<std::string>{"1", "2", "3"}));
+
+  EXPECT_EQ(
+      expected,
+      lambda::map(
+          [](std::string&& s) {
+            return numify<int>(s).get();
+          },
+          std::vector<std::string>{"1", "2", "3"}));
+
+  std::vector<std::string> concat = {"11", "22", "33"};
+
+  EXPECT_EQ(
+      concat,
+      lambda::map(
+          [](std::string&& s) {
+            return s + s;
+          },
+          function()));
+
+  std::vector<OnlyMoveable> v;
+  v.emplace_back(1);
+  v.emplace_back(2);
+
+  std::vector<OnlyMoveable> result = lambda::map(
+      [](OnlyMoveable&& o) {
+        o.j = o.i;
+        return std::move(o);
+      },
+      std::move(v));
+
+  for (const OnlyMoveable& o : result) {
+    EXPECT_EQ(o.i, o.j);
+  }
+}