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
8
votes
2 answers

Python: calling method in the map function

map() and list comprehension are roughly equivalent: map(function, list1) [function(i) for i in list1] What if the function we want to use is a method? [i.function() for i in list1] map(.function, list1) # error! map(run_method(function), list1) #…
Remi.b
  • 17,389
  • 28
  • 87
  • 168
7
votes
5 answers

How to count vowels in Java through functional programming?

I need to count the number of vowels in a list of words in Functional Java. If I have this list: List l = Arrays.asList("hello", "world", "test"); My idea was to "delete" the vowels and then do a subtraction this way: int tot =…
7
votes
1 answer

Apply python lazy map for in-place functions?

Sometimes, I build up a thing by repeatedly applying map, and then having python perform the operations all at once. For example, I could build up a list of lists of ranges like so: foo = [256]*3 foo = map(range, foo) foo = map(list, foo) So far,…
Him
  • 5,257
  • 3
  • 26
  • 83
7
votes
4 answers

How to extract two columns from an array

I'm trying to extract a two dimensional array. The nodes object looks like this I tried to get it with this, but it didn't work. var array_nodes = nodes.forEach(function(d) { return { x: d.x, y: d.y } …
goollan
  • 765
  • 8
  • 19
7
votes
1 answer

map Functor over a list in Haskell

bit confused. fmap sounds like it can map all over a list of Maybe's, but I can't get it to work if I use e.g. fApplyFunctor = (+1) <$> [Just 1, Just 2]. What seems to work perfectly fine is: map ((+1) <$>) [Just 1, Just 2, Just 3]. This seems to…
Madderote
  • 1,107
  • 10
  • 19
7
votes
4 answers

Python map function with multiple arguments to a list of tuples

The heading may sound bizarre but here is what I mean: def f(x, y, z): return a_single_number_from_xyz l = [(10, 'abc', 'def'), (20, 'efg', 'hij')] print sum([ret_value_of_f_from_first_tuple, ret_value_of_f_from_second_tuple]) The three…
QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
7
votes
1 answer

PHP: looking for something like Java Stream API

Is there a way to convert array of objects into an array of strings using some custom mapping in PHP. Like: $objs = array(o1, o2, o3); ... $strings = conv($objs, function($o) -> $o->fieldXYZ); instead of: $objs = array(o1, o2,…
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
7
votes
1 answer

Julia, run function multiple times, save results in array

I am building a microsimulation model in Julia. I have built the structure of my function and it runs great for for 1 "person". I'd like to write the script to run 100000+ people through the model and save the results in one location. Eventually…
MJH
  • 398
  • 2
  • 14
7
votes
3 answers

Lazy evaluation of map

I recently read that one benefit of map in Python 3 was that it is lazy. That means, it is better to do map(lambda x: x**2, range(10**100)) rather than [x**2 for x in range(10**100)] What I'm curious about, is how I can put this laziness to use.…
zephyr
  • 2,182
  • 3
  • 29
  • 51
7
votes
1 answer

Confusion with 'lifting' functions in Scala

In the book Functional Programming In Scala, there's an example of 'Lift' where a function with type A => B is promoted to Option[A] => Option[B]. This is how lift is implemented: def lift[A,B](f: A => B):Option[A] => Option[B] = _ map f I have a…
sc_ray
  • 7,803
  • 11
  • 63
  • 100
6
votes
1 answer

Difference between Python2 and Python3 while using map with find and index

Given a pattern and a string str, find if str follows the same pattern. Here follows means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = "abba", str = "dog cat cat dog"…
user3651247
  • 238
  • 1
  • 7
  • 19
6
votes
4 answers

Best practices for generating N of a component in React (without data)

Let's say I have a component, like: and I want to draw N number of them on the UI. What would the best practice for accomplishing this be? My initial thought is to create an array of N members so I can use map,…
Thomas Murphy
  • 1,360
  • 4
  • 14
  • 41
5
votes
2 answers

Use Map function with the base R pipe |>

How can I use the Map function with the pipe base |>? The next x vector can be used inside the Map function x <- c(1,5,1,2) Map(function(n)n*2, x) |> unlist() # [1] 2 10 2 4 This works, but when I try to use the pipe I get the next error: x |>…
juarpasi
  • 88
  • 6
5
votes
1 answer

Implementing a "Pythonic" map in Scheme: bad idea?

In Scheme, the function (map fn list0 [list1 .. listN]) comes with the restriction that the lists must have the same number of elements. Coming from Python, I'm missing the freedom of Python list comprehensions, which look a lot like map above, but…
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
5
votes
1 answer

How to destructure an array with object that has space in its keys?

data={data.map(({ ID,filePath,accountId,companyId,['First Name'], ...rest }) => rest)} In this case First Name is a key with space, apparently when passed as above it causes error. How to handle this scenario?