reduce

JavaScript reduce can't handle Math functions?

旧巷老猫 提交于 2019-12-12 10:29:39
问题 I'm trying an obvious task: var maxVal = [ 1, 2, 3, 4, 5 ].reduce( Math.max, 0 ); and get: NaN as the result. To make it work I have to make an anonymous function this way: var maxVal = [ 1, 2, 3, 4, 5 ].reduce( function ( a, b ) { return Math.max(a, b); }, 0 ); Could someone tell me why ? Both are functions that take two arguments and both return one value. What's the difference? Another example could be this: var newList = [[1, 2, 3], [4, 5, 6]].reduce( Array.concat, [] ); The result is: [1

stream reduction incompatible types

蓝咒 提交于 2019-12-12 09:48:16
问题 I'm trying to create a finder which takes several predicates and reduces them: public static <T extends BusinessInterface> Collection<T> findOr( Context pContext, Class<T> pClass, Predicate<? super T>... pPredicates) { Predicate<? super T> lReducedPredicate = Arrays.asList(pPredicates).stream().reduce(Predicate::or).orElse(r -> false); return find(pContext, pClass, lReducedPredicate); } Unfortunately I get following compiler error: Predicate lReducedPredicate = Arrays.asList(pPredicates)

How to merge objects attributes from reduce to rereduce function in CouchDB

青春壹個敷衍的年華 提交于 2019-12-12 09:19:56
问题 This is my JSON schema: {"emp_no": .., "salary": .., "from_date": .., "to_date": .., "type" : "salaries"} {"emp_no": .., "title": .., "from_date": .., "to_date" : .., "type" : "titles"} What i wanted to do, is to find the average salary for each active title. Active titles are document with "from_date" attribute set to "9999-01-01" Here is my Map Function function(doc) { if (doc.type == 'salaries') { var dateSalaries = null; dateSalaries = doc.to_date.split("-"); if(dateSalaries[0]

Where is the “Fold” LINQ Extension Method?

Deadly 提交于 2019-12-12 08:17:14
问题 I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example: double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor); Unfortunately, I can't get this to compile, either in their example or in my own code, and I can't find anywhere else in MSDN (like Enumerable or Array extension methods) that mention this method. The error I get is a plain old "don't know anything about that"

Why do I get this error in this omp_declare_reduction?

那年仲夏 提交于 2019-12-12 06:59:11
问题 I have this file: A.h struct B; struct A { A(... B &b) : b(b) {} B &b; }; And B.h : #include "A.h" struct B{ ... std::vector<A> as; } And I have this omp_declare_reduction : #pragma omp declare reduction(merge : B : omp_out.as.insert(omp_out.as.end(), omp_in.as.begin(), omp_in.as.end())) However, I get this error (where A=FindAffineShapeArgs and B=Wrapper ): /usr/include/c++/5/bits/stl_algobase.h(564): error: function "FindAffineShapeArgs::operator=(const FindAffineShapeArgs &)" (declared

Reducing complex filter rules to a comparable form

≯℡__Kan透↙ 提交于 2019-12-12 06:47:30
问题 I want to determine whether or not an input array of integers "matches" a set of rules. The Matcher The Matcher is built from a set of helper methods to describe rules for input data. These rules are essentially logic gates on arrays of integers: AND(1, 2) // Requires both 1 AND 2 be present in the input array. OR(3, 4, 5) // Requires that 3 OR 4 OR 5 be present in the input array. NOR(6, 7) // Requires that neither 6 NOR 7 be present in the input array. XOR(8, 9) // Requires that either 8 (X

What are the consequences of mutating the array while applying Array.reduce to it

北城余情 提交于 2019-12-12 05:29:09
问题 Suppose I have an array: const ar = [1,2,3,4]; And I apply reduce function to it and inside that function I remove elements like this: ar.reduce((result, element, index, original)=>{ original.pop(); }, []); The function will be executed only two times for the first two elements. That's understandable since I've removed 3rd and 4th elements during previous calls. But what is interesting is that if I execute the function and remove the current element: ar.reduce((result, element, index,

How to get the Reducer to emit only duplicates

余生长醉 提交于 2019-12-12 05:27:56
问题 I have a Mapper that is going through lots of data and emitting ID numbers as keys with the value of 1. What I hope to accomplish with the MapReduce job is to get a list of all IDs that have been found more than one time across all data, which is a list of duplicate IDs. For example: Mapper emits: abc 1 efg 1 cba 1 abc 1 dhh 1 In this case, you can see that the ID 'abc' has been emitted more than one time by the Mapper. How do I edit this Reducer so that it will only emit the duplicates? i.e.

Using reduce with custom function

允我心安 提交于 2019-12-12 04:55:25
问题 This is an updated (and hopefully simplified) problem to one I posted earlier. I have a custom function, the purpose of which is to optimize spend over a number of weeks, where spend is allocated to periods of high sales activity. I'm happy with the function, but need a way to be able to iterate through the data a number of times. I was hoping to use the 'reduce' function to accomplish this but haven't had much luck. Here is the initial data to feed into the function: sales <- data.frame

TypeError: $scope.array.reduce is not a function

房东的猫 提交于 2019-12-12 02:46:10
问题 I have 3 drop-down-lists that load attributes related to a page. These attributes are: instruments, style, scoring. The attributes are loaded from a service call. Example using instruments: //Get Instruments $http.get('/api/Instrument/GetAllInstruments').success(function (data, status, headers, config) { $scope.instruments = data; }).error(function (data, status, headers, config) { $scope.error = "An Error has occured while fetching Instruments!" + data; }); and the html: <select class="form