reduce

Reduce Hash Values

試著忘記壹切 提交于 2019-12-02 21:57:55
I am having trouble with the syntax for reduce. I have a hash of the following format: H = {"Key1" => 1, "Key2" => 2} I would like to use reduce to find the sum of the values in this function. Something Like H.reduce(0) {|memo, elem| memo+=elem} I know this is wrong. I dont understand how I can make elem the value of the hash. Use Enumerable#reduce , if you're ok with getting nil if the hash happens to be empty: H.values.reduce(:+) # => 3 Hash.new.values.reduce(:+) # => nil To safely get 0 when the hash is empty, use: H.values.reduce(0) { |sum,x| sum + x } # or... H.reduce(0) { |sum,(key,val)|

Map and Reduce Monad for Clojure… What about a Juxt Monad?

╄→гoц情女王★ 提交于 2019-12-02 21:20:35
Whilst learning Clojure, I've spent ages trying to make sense of monads - what they are and how we can use them.... with not too much success. However, I found an excellent 'Monads for Dummies' Video Series - http://vimeo.com/20717301 - by Brian Marik for Clojure So far, my understanding of monads is that it is sort of like a macro in that it allows a set of statements to be written in a form that is easy to read - but monads are much more formalised. My observations are limited to two examples: 1. The Identity Monad (or the 'let' monad) taken from http://onclojure.com/2009/03/05/a-monad

Mapping values from two array in Ruby

放肆的年华 提交于 2019-12-02 20:26:52
I'm wondering if there's a way to do what I can do below with Python, in Ruby: sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data)) I have two arrays of equal sizes with the weights and data but I can't seem to find a function similar to map in Ruby, reduce I have working. @Michiel de Mare Your Ruby 1.9 example can be shortened a bit further: weights.zip(data).map(:*).reduce(:+) Also note that in Ruby 1.8, if you require ActiveSupport (from Rails) you can use: weights.zip(data).map(&:*).reduce(&:+) In Ruby 1.9: weights.zip(data).map{|a,b| a*b}.reduce(:+) In Ruby 1.8:

JavaScript array .reduce with async/await

半世苍凉 提交于 2019-12-02 17:53:20
Seem to be having some issues incorporating async/await with .reduce(), like so: const data = await bodies.reduce(async(accum, current, index) => { const methodName = methods[index] const method = this[methodName] if (methodName == 'foo') { current.cover = await this.store(current.cover, id) console.log(current) return { ...accum, ...current } } return { ...accum, ...method(current.data) } }, {}) console.log(data) The data object is logged before the this.store completes... I know you can utilise Promise.all with async loops, but does that apply to .reduce() ? Bergi The problem is that your

How does reduce_sum() work in tensorflow?

て烟熏妆下的殇ゞ 提交于 2019-12-02 16:43:51
I am learning tensorflow, I picked up the following code from the tensorflow website. According to my understanding, axis=0 is for rows and axis=1 is for columns. How are they getting output mentioned in comments? I have mentioned output according to my thinking against ##. import tensorflow as tf x = tf.constant([[1, 1, 1], [1, 1, 1]]) tf.reduce_sum(x, 0) # [2, 2, 2] ## [3, 3] tf.reduce_sum(x, 1) # [3, 3] ##[2, 2, 2] tf.reduce_sum(x, [0, 1]) # 6 ## Didn't understood at all. x has a shape of (2, 3) (two rows and three columns): 1 1 1 1 1 1 By doing tf.reduce_sum(x, 0) the tensor is reduced

Reducing/Grouping an array in Javascript

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 14:51:11
Based on this example, I want to group by object in a slightly other way. The outcome should be as follows: [{ key: "audi" items: [ { "make": "audi", "model": "r8", "year": "2012" }, { "make": "audi", "model": "rs5", "year": "2013" } ] }, ... ] How can I achieve that? The following code I wrote doesn't do what I want: reduce(function (r, a) { r[a.art] = {key: r[a.art], items: []} || []; r[a.art].items.push(a); return r; }, Object.create(null)); You could use a hash table for grouping by make and an array for the wanted result. For every group in hash , a new object, like { key: a.make, items:

Reduction with OpenMP: linear merging or log(number of threads) merging

旧城冷巷雨未停 提交于 2019-12-02 14:03:23
问题 I have a general question about reductions with OpenMP that's bothered me for a while. My question is in regards to merging the partial sums in a reduction. It can either be done linearly or as the log of the number of threads. Let's assume I want to do a reduction of some function double foo(int i) . With OpenMP I could do it like this. double sum = 0.0; #pragma omp parallel for reduction (+:sum) for(int i=0; i<n; i++) { sum += f(i); } However, I claim that the following code will be just as

RDD split and do aggregation on new RDDs

妖精的绣舞 提交于 2019-12-02 05:36:00
问题 I have an RDD of (String,String,Int) . I want to reduce it based on the first two strings And Then based on the first String I want to group the (String,Int) and sort them After sorting I need to group them into small groups each containing n elements. I have done the code below. The problem is the number of elements in the step 2 is very large for a single key and the reduceByKey(x++y) takes a lot of time. //Input val data = Array( ("c1","a1",1), ("c1","b1",1), ("c2","a1",1),("c1","a2",1), (

Reduction with OpenMP: linear merging or log(number of threads) merging

别来无恙 提交于 2019-12-02 04:04:59
I have a general question about reductions with OpenMP that's bothered me for a while. My question is in regards to merging the partial sums in a reduction. It can either be done linearly or as the log of the number of threads. Let's assume I want to do a reduction of some function double foo(int i) . With OpenMP I could do it like this. double sum = 0.0; #pragma omp parallel for reduction (+:sum) for(int i=0; i<n; i++) { sum += f(i); } However, I claim that the following code will be just as efficient. double sum = 0.0; #pragma omp parallel { double sum_private = 0.0; #pragma omp for nowait

JS: Reduce array to nested objects

老子叫甜甜 提交于 2019-12-02 02:56:58
问题 So I have this array var mapped = [[2016, "October", "Monday", {object}], [2017, "January", "Friday", {object}], [2017, "January", "Wednesday", {object}], [2017, "October", "Monday", {object}]] What I want to accomplish is something like this: [{ "2016": [{ "October": [{ "Monday": [{object}] }] }], }, { "2017": [{ "January": [{ "Friday": [{object}] }, { "Wednesday": [{object}] }] }, { "October": [{ "Monday": [{object}] }] }] }] I've been searching around for so long, and I can't find a