How to map/reduce/filter a Set in JavaScript?

Is there any way to map/reduce/filter/etc a Set in JavaScript or will I have to write my own? Here’s some sensible Set.prototype extensions Set.prototype.map = function map(f) { var newSet = new Set(); for (var v of this.values()) newSet.add(f(v)); return newSet; }; Set.prototype.reduce = function(f,initial) { var result = initial; for (var v of this) … Read more

NameError: name ‘reduce’ is not defined in Python

I’m using Python 3.2. Tried this: xor = lambda x,y: (x+y)%2 l = reduce(xor, [1,2,3,4]) And got the following error: l = reduce(xor, [1,2,3,4]) NameError: name ‘reduce’ is not defined Tried printing reduce into interactive console – got this error: NameError: name ‘reduce’ is not defined Is reduce really removed in Python 3.2? If that’s … Read more

Javascript reduce() on Object

There is nice Array method reduce() to get one value from the Array. Example: [0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){ return previousValue + currentValue; }); What is the best way to achieve the same with objects? I’d like to do this: { a: {value:1}, b: {value:2}, c: {value:3} }.reduce(function(previous, current, index, array){ return previous.value + current.value; }); … Read more