reduce

Is it valid to reduce on an empty set of sets?

こ雲淡風輕ζ 提交于 2019-12-05 17:43:01
问题 Shouldn't this work? > val setOfSets = Set[Set[String]]() setOfSets: scala.collection.immutable.Set[Set[String]] = Set() > setOfSets reduce (_ union _) java.lang.UnsupportedOperationException: empty.reduceLeft at scala.collection.TraversableOnce$class.reduceLeft(TraversableOnce.scala:152) [...] 回答1: Reduce (left and right) cannot be applied on an empty collection. Conceptually: myCollection.reduce(f) is similar to: myCollection.tail.fold( myCollection.head )( f ) Thus the collection must have

reducelist in Python: like reduce but giving the list of intermediate results

瘦欲@ 提交于 2019-12-05 17:36:34
问题 You know the handy reduce function in Python. For example, you could use it to sum up a list like so (pretend there isn't the built-in sum): reduce(lambda x,y: x+y, [1,2,3,4], 0) which returns (((0+1)+2)+3)+4 = 10. Now what if I wanted a list of the intermediate sums? In this case, [1,3,6,10] . Here's an ugly solution. Is there something more pythonic? def reducelist(f, l, x): out = [x] prev = x for i in l: prev = f(prev, i) out.append(prev) return out 回答1: My favourite, if you're recent

Java stream reduce

感情迁移 提交于 2019-12-05 17:17:56
问题 I have the following example data set that I want to transform / reduce using Java stream api based on direction's value Direction int[] IN 1, 2 OUT 3, 4 OUT 5, 6, 7 IN 8 IN 9 IN 10, 11 OUT 12, 13 IN 14 to Direction int[] IN 1, 2, OUT 3, 4, 5, 6, 7 IN 8, 9, 10, 11 OUT 12, 13 IN 14 code that I've written so far enum Direction { IN, OUT } class Tuple { Direction direction; int[] data; public Tuple merge(Tuple t) { return new Tuple(direction, concat(getData(), t.getData())); } } private static

Swift 3 - Reduce a collection of objects by an Int property

柔情痞子 提交于 2019-12-05 12:42:12
I have an array containing 3 objects like so: class AClass { var distance: Int? } let obj0 = AClass() obj0.distance = 0 let obj1 = AClass() obj1.distance = 1 let obj2 = AClass() obj2.distance = 2 let arr = [obj0, obj1, obj2] When I reduce the array and assign it to a variable, I can only sum the last element in the array. let total = arr.reduce(0, {$1.distance! + $1.distance!}) //returns 4 If I try $0.distance! it errors with "expression is ambiguous without more context". I tried being more explicit: var total = arr.reduce(0, {(first: AClass, second: AClass) -> Int in return first.distance! +

Using array.reduce method to count duplicate elements [duplicate]

[亡魂溺海] 提交于 2019-12-05 12:29:14
This question already has answers here : Word Frequency Count, fix a bug with standard property (3 answers) Closed 5 years ago . I'm working through a tutorial on functional JS and I'm up to a challenge requiring the use of the reduce method: Given a random array of words, output an array that shows the word plus it's word count, for instance: ['apple, 'orange, 'grape', 'apple'] -> ['apple: 2','orange: 1', 'grape: 1] I know this isn't the correct use of reduce, but here was my semi-working solution: var wordCountsArray = inputWords.map(function(item){ var counter = 0; var itemCount =

IE/JS: reduce on an object

一笑奈何 提交于 2019-12-05 03:39:19
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 reason, why IE stops processing the function at this point? And: how to handle this at IE? Thank you.

Using Java 8 Stream Reduce to return List after performing operation on each element using previous elements values

陌路散爱 提交于 2019-12-05 01:55:12
问题 I'm new to Streams and Reduce so I'm trying it out and have hit a problem: I have a list of counters which have a start counter and end counter. The startcounter of an item is always the endcounter of the previous. I have a list of these counters listItems which I want to loop through efficiently, filter out inactive records and then reduce the list into a new List where all the StartCounters are set. I have the following code: List<CounterChain> active = listItems.stream() .filter(e -> e

Video Compression ios : Reduce the size of a video usin AVAssetWriter

老子叫甜甜 提交于 2019-12-04 23:44:58
问题 I am Successful in encoding a series of image captured by AVCaptureVideoPreviewLayer of AVFoundation Framework into a Video using AVAssetWriter and AVAssetWriterInput. But the size of the video is too large, Can anyone of you suggest me a tutorial or atleast a direct me in this case or give me the correct Video output Settings for video compression. I am using the following VideoOutputSetting. videoOutputSettings = @{ AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: [NSNumber numberWithInt

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

喜夏-厌秋 提交于 2019-12-04 19:40:53
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].localeCompare("9999") == 0){ emit(doc.emp_no, ["salary", doc.salary] ); } } else if (doc.type == 'titles') { var

Thrust: How to directly control where an algorithm invocation executes?

混江龙づ霸主 提交于 2019-12-04 18:58:13
The following code has no information that may lead it to run at CPU or GPU. I wonder where is the "reduce" operation executed? #include <thrust/iterator/counting_iterator.h> ... // create iterators thrust::counting_iterator<int> first(10); thrust::counting_iterator<int> last = first + 3; first[0] // returns 10 first[1] // returns 11 first[100] // returns 110 // sum of [first, last) thrust::reduce(first, last); // returns 33 (i.e. 10 + 11 + 12) Furthermore, thrust::transform_reduce( thrust::counting_iterator<unsigned int>(0), thrust::counting_iterator<unsigned int>(N), MyOperation(data), 0