reduce

Why does reduceRight return NaN in Javascript?

泪湿孤枕 提交于 2019-12-23 04:35:28
问题 I'm using Firefox 3.5.7 and within Firebug I'm trying to test the array.reduceRight function, it works for simple arrays but when I try something like that I get a NaN . Why? >>> var details = [{score : 1}, {score: 2}, {score: 3}]; >>> details [Object score=1, Object score=2, Object score=3] >>> details.reduceRight(function(x, y) {return x.score + y.score;}, 0) NaN I also tried map and at least I can see the .score component of each element: >>> details.map(function(x) {console.log (x.score);

Reduce not giving consistent results

心已入冬 提交于 2019-12-23 04:22:48
问题 I am trying to implement my own reduce sum for big 1D arrays. I came up with a reduce kernel and a way to call the kernel several times for step by step reduction to reach a single value. Now I know that this is not the optimal way of computing this (if you see my code it may get to a point where a kernel call needs to be made to add 3 values), but let's obviate that for a moment and try to work with this. Basically, in short: I call the reduce kernel to each time reduce MAXTHREADS times, in

Re-sort a result of reduced view

我怕爱的太早我们不能终老 提交于 2019-12-23 02:57:23
问题 consider the following document structures: Thread: - doc_type 1 - _id - subject (string) Posts: - doc_type 2 - _id - thread_id (_id of Thread) - time (milliseconds since 1970) - comment (string) I need the threads sorted by the last post on a thread, together with latest 5 posts. I thought to avoid updating the thread document every time a new post is done in order to eliminate probability of conflicts in a distributed environment across db nodes. Besides, it will be working for the DB where

How to avoid intermediate results when performing array iterations?

混江龙づ霸主 提交于 2019-12-22 12:38:30
问题 When working with arrays, intermediate representations are needed regularly - particularly in connection with functional programming, in which data is often treated as immutable: const square = x => x * x; const odd = x => (x & 1) === 1; let xs = [1,2,3,4,5,6,7,8,9]; // unnecessary intermediate array: xs.map(square).filter(odd); // [1,4,9,16,25,36,49,64,81] => [1,9,25,49,81] // even worse: xs.map(square).filter(odd).slice(0, 2); // [1,9] How can I avoid this behavior in Javascript/Ecmascript

Difference between fold and reduce revisted

こ雲淡風輕ζ 提交于 2019-12-22 11:45:15
问题 I've been reading a nice answer to Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)? provided by samthebest and I am not sure if I understand all the details: According to the answer ( reduce vs foldLeft ): A big big difference (...) is that reduce should be given a commutative monoid, (...) This distinction is very important for Big Data / MPP / distributed computing, and the entire reason why reduce even exists. and Reduce is defined

Why does Java stream map reduce count my result twice?

耗尽温柔 提交于 2019-12-22 10:59:18
问题 I have this code a: ComparisonResults comparisonResults = requestsList .stream() .map(item -> getResponse(item)) .map(item -> compareToBl(item)) .reduce(new ComparisonResults(), (result1, result2) -> { result1.addSingleResult(result2); // return result1; return new ComparisonResults(result1); }); and this code b: ComparisonResults comparisonResults = requestsList .parallelStream() .map(item -> getResponse(item)) .map(item -> compareToBl(item)) .reduce(new ComparisonResults(), (result1,

when calculate a^b why parallel not work but parallelStream could

烈酒焚心 提交于 2019-12-22 09:56:27
问题 I want to calculate a^b , e.g. 2^30, public long pow(final int a, final int b) first I used this manner return LongStream.range(0, b).reduce(1, (acc, x) -> a * acc); // 1073741824 Got right result. Then I want to calculate it parallelly, so naturally I changed it to return LongStream.range(0, b).parallel().reduce(1, (acc, x) -> a * acc); // 32 but in this case the result is just 32 . Why? So for supporting parallel I changed it again return Collections.nCopies(b,a).parallelStream().reduce(1,

IE/JS: reduce on an object

不羁岁月 提交于 2019-12-22 04:45:12
问题 my javascript Application works on firefox and chrome very well. But it seams to be broken on Internet Explorer (IE 8). I did not get an error Message on the console-log. By debugging the code I notice, that the application breaks on the following line: series.reduce(visit, []); The whole function exits at this point. I know, that reduce works for arrays, but console.info(typeof(series)) tells: object But this object exactly looks like an array - and it works on FF/Chrome. Could this be the

Hadoop: Reducer writing Mapper output into Output File

我只是一个虾纸丫 提交于 2019-12-21 18:58:13
问题 I met a very very strange problem. The reducers do work but if I check the output files, I only found the output from the mappers. When I was trying to debug, I found the same problem with the word count sample after I changed the mappers' output value type from Longwritable to Text package org.myorg; import java.io.IOException; import java.util.*; import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.*; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.*; import org

Hadoop: Reducer writing Mapper output into Output File

我们两清 提交于 2019-12-21 18:58:06
问题 I met a very very strange problem. The reducers do work but if I check the output files, I only found the output from the mappers. When I was trying to debug, I found the same problem with the word count sample after I changed the mappers' output value type from Longwritable to Text package org.myorg; import java.io.IOException; import java.util.*; import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.*; import org.apache.hadoop.io.*; import org.apache.hadoop.mapreduce.*; import org