reduce

Angular 2 pipe - calculating summary of array of objects

梦想的初衷 提交于 2019-12-10 13:28:46
问题 I have list of objects with balances (there are other properties in objects but not imported for example): [{ balance : 100 },{ balance : 200 },{ balance : null },{ balance : 300 }] I am looking for smart pipe that would sum (other would average) balances in array (would prefer not using for loop - but some ES6 functionality like reduce but not sure how) 回答1: You will need to write your own pipe, below should give you what you are after. It takes the attribute of the object you want to sum as

Getting the sum of an array of doubles in swift

烈酒焚心 提交于 2019-12-10 12:18:49
问题 I have an array of string numbers that I am fetching from core data and converting to doubles. I would like to get their sum once I've done this, but I get an error. I've tried it like this: override func viewDidLoad() { super.viewDidLoad() //CoreData let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate let managedContext : NSManagedObjectContext = appDelegate.managedObjectContext! var fetchRequest = NSFetchRequest(entityName: "Log") fetchRequest.returnsObjectsAsFaults

Reduce returns nil on summation

拟墨画扇 提交于 2019-12-10 12:18:49
问题 I'm counting the number of times an item appears in an Enumeration. irb(main):003:0> (1..3).reduce(0) {|sum, p| sum += 1 if p == 1} => nil irb(main):004:0> (1..3).find_all{|p| p == 1}.length => 1 The reduce method seems like it should have the same behaviour as the find_all method. Why does it return nil instead of 1 ? irb(main):023:0> (1..3).reduce(0) {|sum, p| sum += 1 if p == 2} NoMethodError: undefined method `+' for nil:NilClass from (irb):23:in `block in irb_binding' from (irb):23:in

UIImage reduce byte size

本小妞迷上赌 提交于 2019-12-10 10:59:28
问题 I am using following code to resize an image - it all works well and as expected... Resize UIImage the right way I use interpolation quality as kCGInterpolationLow and UIImageJPEGRepresentation(image,0.0) to get the NSData of that image. The problem is that the image size is still a bit high in size at around 100kb. My question is can I reduce it further? The images originate from the iPhone Photo Album and are selected via a imagePickerController . Many thanks, 来源: https://stackoverflow.com

How to re-create Underscore.js _.reduce method?

风流意气都作罢 提交于 2019-12-10 10:49:23
问题 For education purposes, I was trying to re-create Underscore.js's _.reduce() method. While I was able to do this in an explicit style using for loops. But this is far from ideal because it mutates the original list that was supplied as an argument, which is dangerous. I also realized that creating such method using functional programming style is harder, since it is not possible to explicitly set i value for looping. // Explicit style var reduce = function(list, iteratee, initial) { if (Array

Spark dataframe reduceByKey

痴心易碎 提交于 2019-12-10 10:25:50
问题 I am using Spark 1.5/1.6, where I want to do reduceByKey operation in DataFrame, I don't want to convert the df to rdd. Each row looks like and I have multiple rows for id1. id1, id2, score, time I want to have something like: id1, [ (id21, score21, time21) , ((id22, score22, time22)) , ((id23, score23, time23)) ] So, for each "id1", I want all records in a list By the way, the reason why don't want to convert df to rdd is because I have to join this (reduced) dataframe to another dataframe,

group by, and sum, and generate a object for each array javascript

做~自己de王妃 提交于 2019-12-10 09:43:30
问题 i need help with this, i need group by id and sum, but i need a new object for each result let data =[ {"id":"2018", "name":"test", "total":1200}, {"id":"2019", "name":"wath", "total":1500}, {"id":"2019", "name":"wath", "total":1800}, {"id":"2020", "name":"zooi", "total":1000}, ] i have this code that return just one object with the result let result = data.reduce(function (r, o) { (r[o.id])? r[o.id] += o.total: r[o.id] = o.total; return r; }); but i need some like this [ {"id":"2018", "name"

Implement fibonacci in Clojure using map/reduce

徘徊边缘 提交于 2019-12-09 14:16:45
问题 Is it possible to implement the fibonacci series in Clojure efficiently using reduce ? What would the "accumulator" contain? I imagine that it will have to be lazy. It's obvious how to do it using recursion or loop/recur. 回答1: You could use a pair of successive fibonacci values as the accumulator, as follows: (reduce (fn [[a b] _] [b (+ a b)]) ; function to calculate the next pair of values [0 1] ; initial pair of fibonnaci numbers (range 10)) ; a seq to specify how many iterations you want =

python: union keys from multiple dictionary?

我只是一个虾纸丫 提交于 2019-12-09 05:17:51
问题 I have 5 dictionaries and I want a union of their keys. alldict = [dict1, dict2, dict3, dict4, dict5] I tried allkey = reduce(lambda x, y: set(x.keys()).union(y.keys()), alldict) but it gave me an error AttributeError: 'set' object has no attribute 'keys' Am I doing it wrong ? I using normal forloop but I wonder why the above code didn't work. 回答1: Your solution works for the first two elements in the list, but then dict1 and dict2 got reduced into a set and that set is put into your lambda

What does the Reduce() JavaScript function do?

末鹿安然 提交于 2019-12-09 03:36:25
问题 I found very useful function reduce() , and I'm using it, but I'm not sure if I understand it properly. Can any one help me to understand this function? Example: var arr = [1,2,3,4,5,6]; arr.reduce(function(p,n){ return p+n; },0); // Output 21 This is my understanding: Reduce() loop through every element of the array and returning previous + current value . Ex. 0+1, 1+2 etc. In this case this function will return [0] - return 1; [1] - return 3; [2] - return 5, [3] - return 7, [4] - return 9,