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
53
votes
4 answers

What's the most idiomatic way of working with an Iterator of Results?

I have code like this: let things = vec![/* ...*/]; // e.g. Vec things .map(|thing| { let a = try!(do_stuff(thing)); Ok(other_stuff(a)) }) .filter(|thing_result| match *thing_result { Err(e) => true, …
Tim McLean
  • 1,576
  • 3
  • 14
  • 20
50
votes
5 answers

STL name for the "map" functional programming function

I would like to be able to write something like char f(char); vector bar; vector foo = map(f, bar); The transform function appears to be similar, but it will not autogenerate the size of the resultant collection.
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
48
votes
5 answers

Using map() function with keyword arguments

Here is the loop I am trying to use the map function on: volume_ids = [1,2,3,4,5] ip = '172.12.13.122' for volume_id in volume_ids: my_function(volume_id, ip=ip) Is there a way I can do this? It would be trivial if it weren't for the ip…
Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31
43
votes
4 answers

Function application over numpy's matrix row/column

I am using Numpy to store data into matrices. Coming from R background, there has been an extremely simple way to apply a function over row/columns or both of a matrix. Is there something similar for python/numpy combination? It's not a problem to…
petr
  • 2,554
  • 3
  • 20
  • 29
43
votes
10 answers

Array.map 1 element to multiple element

I have [3, 16, 120]. when I do [3, 16, 120].map(mapper), I want to achieve, for example [4,5, 17,18, 121,122] i.e. each element map to n+1 and n+2. This is of course an example - what I want is to simply push multiple values from mapper…
Boyang
  • 2,520
  • 5
  • 31
  • 49
41
votes
2 answers

Python 'map' function inserting NaN, possible to return original values instead?

I am passing a dictionary to the map function to recode values in the column of a Pandas dataframe. However, I noticed that if there is a value in the original series that is not explicitly in the dictionary, it gets recoded to NaN. Here is a simple…
atkat12
  • 3,840
  • 7
  • 22
  • 22
40
votes
1 answer

Prolog map procedure that applies predicate to list elements

How do you write a Prolog procedure map(List, PredName, Result) that applies the predicate PredName(Arg, Res) to the elements of List, and returns the result in the list Result? For example: test(N,R) :- R is N*N. ?- map([3,5,-2], test, L). L =…
General_9
  • 2,249
  • 4
  • 28
  • 46
37
votes
6 answers

Can I use index information inside the map function?

Let's assume there is a list a = [1, 3, 5, 6, 8]. I want to apply some transformation on that list and I want to avoid doing it sequentially, so something like map(someTransformationFunction, a) would normally do the trick, but what if the…
LetsPlayYahtzee
  • 7,161
  • 12
  • 41
  • 65
36
votes
4 answers

Python `map` and arguments unpacking

I know, that map(function, arguments) is equivalent to for argument in arguments: function(argument) Is it possible to use map function to do the following? for arg, kwargs in arguments: function(arg, **kwargs)
Radosław Miernik
  • 4,004
  • 8
  • 33
  • 36
35
votes
3 answers

Using multiple map functions vs. a block statement in a map in a java stream

Say I have the following code data.stream() .map(x -> { Object a = maybeReturnsNull(x); return a == null ? defaultValue : a; }) I have some function that might be returning null, and I'm applying it to an element of the…
Brian Ecker
  • 2,017
  • 1
  • 21
  • 30
35
votes
5 answers

python : can reduce be translated into list comprehensions like map, lambda and filter?

When programming in python, I now avoid map, lambda and filter by using list comprehensions because it is easier to read and faster in execution. But can reduce be replaced as well? E.g. an object has an operator union() that works on another…
Eric H.
  • 2,152
  • 4
  • 22
  • 34
34
votes
5 answers

Python: Difference between filter(function, sequence) and map(function, sequence)

I'm reading through the Python documentation to really get in depth with the Python language and came across the filter and map functions. I have used filter before, but never map, although I have seen both in various Python questions here on SO.…
samrap
  • 5,595
  • 5
  • 31
  • 56
31
votes
5 answers

Python: Something like `map` that works on threads

I was sure there was something like this in the standard library, but it seems I was wrong. I have a bunch of urls that I want to urlopen in parallel. I want something like the builtin map function, except the work is done in parallel by a bunch of…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
31
votes
3 answers

Python 3 Map function is not Calling up function

Why doesn't following code print anything: #!/usr/bin/python3 class test: def do_someting(self,value): print(value) return value def fun1(self): map(self.do_someting,range(10)) if __name__=="__main__": t =…
Sibi
  • 47,472
  • 16
  • 95
  • 163
27
votes
2 answers

Condition in map function

Is there anything in Scala like, condition ? first_expression : second_expression; that I can use within map function in scala? I want to be able to write something like this: val statuses = tweets.map(status => status.isTruncate? //do nothing |…
Lisa
  • 3,121
  • 15
  • 53
  • 85
1
2
3
55 56