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

What does duckmap really do?

From the documentation duckmap will apply &block on each element and return a new list with defined return values of the block. For undefined return values, duckmap will try to descend into the element if that element implements Iterable. But…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
11
votes
5 answers

why i can't reverse a list of list in python

i wanted to do something like this but this code return list of None (i think it's because list.reverse() is reversing the list in place): map(lambda row: row.reverse(), figure) i tried this one, but the reversed return an iterator : map(reversed,…
mouad
  • 67,571
  • 18
  • 114
  • 106
11
votes
6 answers

Using Array.map with new Array constructor

I was trying to use new Array() constructor with map in order to create a one-line code that creates a list of elements. Something like this : let arr = new Array(12).map( (el, i) => { console.log('This is never called'); return i +…
Jose Hermosilla Rodrigo
  • 3,513
  • 6
  • 22
  • 38
10
votes
9 answers

Is there a value in using map() vs for?

Does map() iterate through the list like "for" would? Is there a value in using map vs for? If so, right now my code looks like this: for item in items: item.my_func() If it makes sense, I would like to make it map(). Is that possible? What is…
roder
  • 566
  • 6
  • 13
10
votes
3 answers

Return an element using `array.some()` instead of boolean

I'm trying to use an array.some function to iterate through some data and return my field if the if statement succeeds. What I am finding is happening instead, is that I am getting a boolean return e.g true instead of the actual variable (which…
user1486133
  • 1,309
  • 3
  • 19
  • 37
10
votes
4 answers

Mapping Array in Javascript with sequential numbers

The following code: let myArray = Array.apply(null, {length: 10}).map(Number.call, Number); Creates the following Array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] I just don't understand why. I can't find anything on the internet that explains this…
Jeremy Iglehart
  • 4,281
  • 5
  • 25
  • 38
9
votes
3 answers

Why is this JavaScript map NOT an Infinite Loop?

I'm learning JavaScript. I wrote this code to learn the map function. But then I got confused as to why is this not mapping over it continuously as with each map sequence a new element is pushed to the array. Shouldn't it continue to push new…
ExpertNoob
  • 149
  • 1
  • 9
9
votes
4 answers

Python; exception aware map()

I'm doing some pyplotting of generic data and converting it from a power value to a dB value. Due to the system these values come from, 0 is used as a 'the useful data ends here' indicator (the nature of the mathematics, not a defined value). My…
Bolster
  • 7,460
  • 13
  • 61
  • 96
9
votes
1 answer

Filter a simple array with JMESPath

I'm trying to filter a plain list I get from the Azure CLI, and am struggling to construct a query that filters the list properly. An example which encapsulates what I'm trying to accomplish would be trying to filter the list [1, 2, 3, 4, 5] and…
Marcus
  • 3,216
  • 2
  • 22
  • 22
9
votes
2 answers

Exclude null values in map function of Python3

I am using map to process a list in Python3.6: def calc(num): if num > 5: return None return num * 2 r = map(lambda num: clac(num), range(1, 10)) print(list(r)) # => [2, 4, 6, 8, 10, None, None, None, None] The result I expect…
Martin Zhu
  • 421
  • 3
  • 13
9
votes
3 answers

Conditional Statement in a map function with es6

I nee to use the conditional statement in a map function I am duplicating each single value of a path d in a SVG but i do not want this happening for the objects M and L of the array Here is an example of the array as string. M 175 0 L…
Koala7
  • 1,340
  • 7
  • 41
  • 83
9
votes
6 answers

Array.map want to map a value to nothing

I have the array: [1,2,3,4,5,6,7] I want to achieve: [[1,2], [3,4], [5,6], [7]] I'm thinking Array.map, but it doesn't seem to be able to map to nothing for an element? I have (using Underscorejs): arr.map(function(el, idx, arr) { if (idx%2 != 0)…
Boyang
  • 2,520
  • 5
  • 31
  • 49
8
votes
1 answer

Difference between Array(n) and [...Array(n)]

Why there is a difference in map() output in the below code? var y = [1,2,2,1]; var t = y.map(ind => [...Array(ind)].map((_,i) => ind+""+i)); // This makes [ [ '10' ], [ '20', '21' ], [ '20', '21' ], [ '10' ] ] var t1 = y.map(ind =>…
Veeshoo
  • 185
  • 4
8
votes
5 answers

Map over list, except for last list element

How do I best map over all elements of a list, except for the last list element? Say we have a list let l = [1,2,3,4] and want to get [2,3,4,4]. I do have a solution, but it doesn't feel like the "functional" way to do it (in ghci): let l =…
mort
  • 12,988
  • 14
  • 52
  • 97
8
votes
3 answers

Is it possible to define foldr using map?

After I defined map using foldr a question came to my mind: If it is possible to define map using foldr, what about the opposite? From my point of view it is not possible, but I can't find a proper explanation. Thanks for the help!
Josh
  • 83
  • 5