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

Why does new Array(3) function not return 3 undefined values in an Array?

I am trying to create multiple div tags based on the value passed on to the function. ISo to do that in the function I create a new Array(x), Supposedly it should create an Array with x number of undefined pointers. But while console.log(theArray).…
jatin grover
  • 396
  • 1
  • 2
  • 10
4
votes
4 answers

Equivalent of List Comprehension Using Map and Filter

I want to write this code using a map and/or a filter function. It returns the indices of items in a list provided the sum up to a target I have used list comprehension for this but can't see how to get the second for loop into the map/filter…
4
votes
4 answers

Sort Map in Ascending Order by Key

I'm attempting to sort a Map in ascending order based on the keys. Given the Map: Map map = new LinkedHashMap(); map.put(5, "five"); map.put(1, "one"); map.put(3, "three"); map.put(0, "zero"); I would like the…
Alan Cook
  • 342
  • 3
  • 14
4
votes
2 answers

Haskell Function Composition with Map Function

I'm going through the Richard Bird's "Thinking Functionally with Haskell" book and there is a section that I can't understand where he's proving a property of the filter method. What he's proving is: filter p . map f = map f . filter (p .…
flopoe
  • 165
  • 1
  • 9
4
votes
6 answers

Find if any item in the array matches the condition

I am new to Javascript. Now, Here I have an array which has multiple objects. So, I want to iterate it and if any of the object matches the condition then I want to return a value and stop that loop. My array of obj is like, var obj = [ { type: "",…
ganesh kaspate
  • 1
  • 9
  • 41
  • 88
4
votes
1 answer

Usage of .map with defaultdict

I have a pandas dataframe and I have to fill a new column based on the values of an existing column, associating the values of a dictionary. mydict={'key1':'val1', 'key2':'val2'} df['new_col']=df['keys'].map(mydict) Now I have a similar problem,…
sato
  • 768
  • 1
  • 9
  • 30
4
votes
2 answers

How to update state in map function in reactjs

I am having 4 buttons each button have name id and selected boolean flag. What I am trying to achieve is, on click of button, boolean button flag should be changed of that particular button. For this, I need to setState in map function for that…
Mohammed Sabir
  • 157
  • 2
  • 2
  • 17
4
votes
2 answers

Java streams map with sideeffect and collect or foreach and populate a result-list

I have a piece of code that looks something like this. I have read two contradicting(?) "rules" regarding this. That .map should not have side effects That .foreach should not update a mutable variable (so if I refactor to use foreach…
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
4
votes
1 answer

Why does Dictionary.map return an array of tuples, and where is it documented?

Consider the code below: let dict = [ "key1" : 1, "key2" : 2, "key3" : 3, "key4" : 4, "key5" : 5 ] let array = dict.map{$0} for item in array { print(item) } What you get from the print statements is: ("key2", 2) ("key3", 3) ("key4",…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
4
votes
1 answer

How do you run map on a Javascript Set?

Javascript has an Array.prototype.map but there doesn't seem to be an equivalent for sets. What's the recommended way to run map on a set in Javascript? EDIT: Adding an example as requested users = new Set([1,2,3]) // throws an error: // Uncaugh…
k26dr
  • 1,229
  • 18
  • 15
4
votes
2 answers

How to use map with a function that needs more arguments

I am trying to use map with (string-split "a,b,c" ",") to split strings in a list. (string-split "a,b,c" ",") '("a" "b" "c") Following works if string-split is used without ",": (define sl (list "a b c" "d e f" "x y z")) (map string-split…
rnso
  • 23,686
  • 25
  • 112
  • 234
4
votes
1 answer

Transform an array of strings to an array of objects with two intermediary $.getJSON calls

Input: an array of username strings Needed output: an array of Javascript Objects that correspond to each username in the input. The properties for these JS objects is to be built from two API calls for each username (I am using $.getJSON calls.…
Kevin Hernandez
  • 522
  • 1
  • 6
  • 20
4
votes
3 answers

Java 8: Convert a map with string values to a list containg a different type

I have this Map: Map> map = ... And I have this class Foo: class Foo { int id; String name; } I want to convert the map to List. Is there a convenient manner in Java 8 to do this? Currently, my way is: List
zhuguowei
  • 8,401
  • 16
  • 70
  • 106
4
votes
3 answers

map with lambda vs map with function - how to pass more than one variable to function?

I wanted to learn about using map in python and a google search brought me to http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php which I have found helpful. One of the codes on that page uses a for loop and puts map within that for…
Darren Haynes
  • 1,343
  • 4
  • 18
  • 31
3
votes
2 answers

'Map' higher order Haskell function

I have a list, for example: ["Hello", "Goodbye"] and I want to use map on the list; I've successfully used map before: f = ("example" ++) so then: map f ["Hello", "Goodbye"] Would make the list: ["exampleHello", "exampleGoodbye"] but how can I…
James Andrew
  • 7,197
  • 14
  • 46
  • 62