[icinga-checkins] icinga.org: icinga2/master: Change how Array#reduce works

git at icinga.org git at icinga.org
Fri Jul 29 11:10:01 CEST 2016


Module: icinga2
Branch: master
Commit: ee75094128ef1a45ad2f245d1cb6bb2cfb61f47a
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=ee75094128ef1a45ad2f245d1cb6bb2cfb61f47a

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Fri Jul 29 11:09:46 2016 +0200

Change how Array#reduce works

refs #12247

---

 doc/19-library-reference.md |    4 ++--
 lib/base/array-script.cpp   |    9 ++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/doc/19-library-reference.md b/doc/19-library-reference.md
index 99664fb..83efd8c 100644
--- a/doc/19-library-reference.md
+++ b/doc/19-library-reference.md
@@ -708,8 +708,8 @@ Signature:
     function reduce(func);
 
 Reduces the elements of the array into a single value by calling the provided
-function `func` as `func(a, b)` repeatedly where `a` is the previous result of
-function call (null initially) and `b` is an element of the array.
+function `func` as `func(a, b)` repeatedly where `a` and `b` are elements of the array
+or results from previous function calls.
 
 ### <a id="array-filter"> Array#filter
 
diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp
index c048231..f592479 100644
--- a/lib/base/array-script.cpp
+++ b/lib/base/array-script.cpp
@@ -172,14 +172,17 @@ static Value ArrayReduce(const Function::Ptr& function)
 	if (vframe->Sandboxed && !function->IsSideEffectFree())
 		BOOST_THROW_EXCEPTION(ScriptError("Reduce function must be side-effect free."));
 
-	Value result;
+	if (self->GetLength() == 0)
+		return Empty;
+
+	Value result = self->Get(0);
 
 	ObjectLock olock(self);
-	BOOST_FOREACH(const Value& item, self) {
+	for (size_t i = 1; i < self->GetLength(); i++) {
 		ScriptFrame uframe;
 		std::vector<Value> args;
 		args.push_back(result);
-		args.push_back(item);
+		args.push_back(self->Get(i));
 		result = function->Invoke(args);
 	}
 



More information about the icinga-checkins mailing list