Questions tagged [map-function]

Higher-order function "map" transforms a collection by applying a given function to each element in it. USE [maps] for geography, [map] or [dictionary] for data types.

A map function is a higher-order function that applies a function to each element in a list, building a list of the results. Examples include map in Python, Scheme, Haskell and Scala, Stream.map in Java 8, array_map in PHP, and std::transform in C++.

A generalized map works with some general type of a collection, applying a function to each element in the collection and building the collection (of the same general type) of the results. An example is fmap in Haskell, etc.

For questions about geography, use the tag instead.

For questions about data types, use the tag instead, which is a synonym tag for .

829 questions
1768
votes
41 answers

map function for objects (instead of arrays)

I have an object: myObject = { 'a': 1, 'b': 2, 'c': 3 } I am looking for a native method, similar to Array.prototype.map that would be used as follows: newObject = myObject.map(function (value, label) { return value * value; }); // newObject…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
950
votes
14 answers

List comprehension vs map

Is there a reason to prefer using map() over list comprehension or vice versa? Is either of them generally more efficient or considered generally more Pythonic than the other?
TimothyAWiseman
  • 14,385
  • 12
  • 40
  • 47
637
votes
11 answers

Getting a map() to return a list in Python 3.x

I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy: A: Python 2.6: >>> map(chr, [66, 53, 0, 94]) ['B', '5', '\x00', '^'] However, in Python 3.1, the above returns a map object. B: Python 3.1: >>>…
mozami
  • 7,391
  • 4
  • 21
  • 20
348
votes
6 answers

Understanding the map function

The Python 2 documentation says: Built-in Functions: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is…
Web Master
  • 4,240
  • 6
  • 20
  • 28
342
votes
8 answers

Mapping over values in a python dictionary

Given a dictionary { k1: v1, k2: v2 ... } I want to get { k1: f(v1), k2: f(v2) ... } provided I pass a function f. Is there any such built in function? Or do I have to do dict([(k, f(v)) for (k, v) in my_dictionary.iteritems()]) Ideally I would…
Tarrasch
  • 10,199
  • 6
  • 41
  • 57
307
votes
14 answers

Why does the `map` method apparently not work on arrays created via `new Array(count)`?

I've observed this in Firefox-3.5.7/Firebug-1.5.3 and Firefox-3.6.16/Firebug-1.6.2 When I fire up Firebug: var x = new Array(3) console.log(x) // [undefined, undefined, undefined] var y = [undefined, undefined, undefined] console.log(y) //…
rampion
  • 87,131
  • 49
  • 199
  • 315
244
votes
9 answers

Is there a difference between foreach and map?

Ok this is more of a computer science question, than a question based on a particular language, but is there a difference between a map operation and a foreach operation? Or are they simply different names for the same thing?
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
230
votes
8 answers

Are list-comprehensions and functional functions faster than "for loops"?

In terms of performance in Python, is a list-comprehension, or functions like map(), filter() and reduce() faster than a for loop? Why, technically, they run in a C speed, while the for loop runs in the python virtual machine speed?. Suppose that in…
Ericson Willians
  • 7,606
  • 11
  • 63
  • 114
171
votes
6 answers

Java: is there a map function?

I need a map function. Is there something like this in Java already? (For those who wonder: I of course know how to implement this trivial function myself...)
Albert
  • 65,406
  • 61
  • 242
  • 386
151
votes
12 answers

Mapping a function on the values of a map in Clojure

I want to transform one map of values to another map with the same keys but with a function applied to the values. I would think there was a function for doing this in the clojure api, but I have been unable to find it. Here's an example…
Thomas
  • 2,769
  • 5
  • 21
  • 21
141
votes
4 answers

What is the difference between .map, .every, and .forEach?

I've always wondered what the difference between them were. They all seem to do the same thing...
David G
  • 94,763
  • 41
  • 167
  • 253
107
votes
1 answer

"this" is undefined inside map function Reactjs

I'm working with Reactjs, writing a menu component. "use strict"; var React = require("react"); var Menus = React.createClass({ item_url: function (item,categories,articles) { console.log('afdasfasfasdfasdf'); var url='XXX'; …
iamhuy
  • 1,081
  • 2
  • 8
  • 7
105
votes
10 answers

Pass multiple parameters to concurrent.futures.Executor.map?

The concurrent.futures.Executor.map takes a variable number of iterables from which the function given is called. How should I call it if I have a generator that produces tuples that are normally unpacked in place? The following doesn't work because…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
104
votes
7 answers

Map function in MATLAB?

I'm a little surprised that MATLAB doesn't have a Map function, so I hacked one together myself since it's something I can't live without. Is there a better version out there? Is there a somewhat-standard functional programming library for MATLAB…
user121550
87
votes
3 answers

Passing multiple parameters to pool.map() function in Python

I need some way to use a function within pool.map() that accepts more than one parameter. As per my understanding, the target function of pool.map() can only have one iterable as a parameter but is there a way that I can pass other parameters in as…
DJMcCarthy12
  • 3,819
  • 8
  • 28
  • 34
1
2 3
55 56