Mapping a function on the values of a map in Clojure
I like your reduce version just fine. I think it’s idiomatic. Here’s a version using list comprehension anyways. (defn foo [m f] (into {} (for [[k v] m] [k (f v)])))
I like your reduce version just fine. I think it’s idiomatic. Here’s a version using list comprehension anyways. (defn foo [m f] (into {} (for [[k v] m] [k (f v)])))
Since Java 8, there are some standard options to do this in JDK: Collection<E> in = … Object[] mapped = in.stream().map(e -> doMap(e)).toArray(); // or List<E> mapped = in.stream().map(e -> doMap(e)).collect(Collectors.toList()); See java.util.Collection.stream() and java.util.stream.Collectors.toList().
The following are rough guidelines and educated guesses based on experience. You should timeit or profile your concrete use case to get hard numbers, and those numbers may occasionally disagree with the below. A list comprehension is usually a tiny bit faster than the precisely equivalent for loop (that actually builds a list), most likely … Read more
Different. foreach iterates over a list and performs some operation with side effects to each list member (such as saving each one to the database for example) map iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (such as converting a list … Read more
I had a task that I only knew the length of the array and needed to transform the items. I wanted to do something like this: let arr = new Array(10).map((val,idx) => idx); To quickly create an array like this: [0,1,2,3,4,5,6,7,8,9] But it didn’t work because: (see Jonathan Lonowski’s answer a few answers beneath) The … Read more
There is no such function; the easiest way to do this is to use a dict comprehension: my_dictionary = {k: f(v) for k, v in my_dictionary.items()} In python 2.7, use the .iteritems() method instead of .items() to save memory. The dict comprehension syntax wasn’t introduced until python 2.7. Note that there is no such method … Read more
map isn’t particularly pythonic. I would recommend using list comprehensions instead: map(f, iterable) is basically equivalent to: [f(x) for x in iterable] map on its own can’t do a Cartesian product, because the length of its output list is always the same as its input list. You can trivially do a Cartesian product with a … Read more
Do this: list(map(chr,[66,53,0,94])) In Python 3+, many processes that iterate over iterables return iterators themselves. In most cases, this ends up saving memory, and should make things go faster. If all you’re going to do is iterate over this list eventually, there’s no need to even convert it to a list, because you can still … Read more
map may be microscopically faster in some cases (when you’re NOT making a lambda for the purpose, but using the same function in map and a listcomp). List comprehensions may be faster in other cases and most (not all) pythonistas consider them more direct and clearer. An example of the tiny speed advantage of map … Read more
There is no native map to the Object object, but how about this: var myObject = { ‘a’: 1, ‘b’: 2, ‘c’: 3 }; Object.keys(myObject).forEach(function(key, index) { myObject[key] *= 2; }); console.log(myObject); // => { ‘a’: 2, ‘b’: 4, ‘c’: 6 } But you could easily iterate over an object using for … in: var … Read more