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
3
votes
1 answer

Why is `fmap` needed to call `succ` on a `Maybe Integer`?

I am brand new to Haskell and am working on a practice Collatz conjecture problem. The required output is the number of steps needed to get to 1 from a given integer. This is the first time I've had to use the Maybe type, which may be contributing…
arsalanQ
  • 127
  • 6
3
votes
1 answer

Haskell map function for two lists

I need to apply a function to two lists. The map function is map :: (a->b) -> [a] -> [b], however I need something more like map2 :: (a->b->c) -> [a] -> [b] -> [c]. Is there a prelude function similar to map that can do this?
3
votes
1 answer

How can I zip a list over another list that is nested in Haskell?

so, here's a type definition, just for some context: type Name = String type Coordinates = (Int, Int) type Pop = Int type TotalPop = [Pop] type City = (Name, (Coordinates, TotalPop)) And here's a data set: testData :: [City] testData = [("New York…
Alex
  • 394
  • 1
  • 4
  • 15
3
votes
3 answers

What aren't these two ways of expressing map function equivalent?

I got a surprise today while looking at another SO question: let s = "1,a" let arr = s.split(separator: ",") let result = arr.compactMap{Int($0)} // ok let result2 = arr.compactMap(Int.init) // error Why is line 3 legal but line 4 is not? I would…
matt
  • 515,959
  • 87
  • 875
  • 1,141
3
votes
1 answer

Functions in Haskell - Understanding

I have the following code which gives out [2,4,6] : j :: [Int] j = ((\f x -> map x) (\y -> y + 3) (\z -> 2*z)) [1,2,3] Why? It seems that just the "z-function" is being used, what happens to the "y-function"? And how does map work in this…
3
votes
1 answer

Using purrr:map to loop through web pages for scraping with Rselenium

I have a basic R script which I have cobbled together using Rselenium which allows me to log into a website, once authenticated my script then goes to the first page of interest and pulls 3 pieces of text from the page. Luckily for me the URL has…
TheGoat
  • 2,587
  • 3
  • 25
  • 58
3
votes
1 answer

How to change the problem solving format to use lambda and/or map (Python)

I'm trying to code the following formula: To do this I'm using the following function: def predict(x, w): """ Function to predict y(x, w) based on the values of x and w. Args: x: a vector including all the values of the…
brenda
  • 656
  • 8
  • 24
3
votes
3 answers

Unsure how to resolve a pending promise?

I'm looping through some data and fetching individual navigation items. If I console.log(navItems) before the return in the getNavItems function, it logs everything I expect. However, if I log footerData before my return inside getFooterData, I'm…
Nick
  • 2,261
  • 5
  • 33
  • 65
3
votes
3 answers

map function to return 1 boolean value, instead of an array of boolean values

Say you have an array like this: arrayExample = [1, 2, 3, 4, 5, 6, 7] I want to use the map function to iterate through arrayExample and return true if all the numbers are less than 8, false if they are not. However, when I do this I get an array…
webdesignnoob
  • 381
  • 1
  • 4
  • 13
3
votes
3 answers

Filter over [Maybe a] and discard Nothing values

With a list of Maybe a, how to filter and take only the elements of the list which are not Nothing? -- input pas = [Just 3, Just 1, Just 5, Just 9, Nothing, Just 10, Nothing] :: [Maybe Int] -- expected output new_pas = [3, 1, 5, 9, 10] I've tried…
Jivan
  • 21,522
  • 15
  • 80
  • 131
3
votes
1 answer

Haskell - How to create a mapTree function based on a foldr of a BinaryTree?

This is a question from Chapter 11, Algebraic Datatypes of "Haskell Programming from first principles": data BinaryTree a = Leaf | Node (BinaryTree a) a (BinaryTree a) deriving (Eq, Ord, Show) We do not actually insert a value into an…
maxloo
  • 453
  • 2
  • 12
3
votes
3 answers

Adding a class to first n elements in React map function dynamically

I am mapping through an array for which I want to assign a class to the first n elements. The first n elements are passed by the parent component and is steadily increasing. I could use something like a ternary operator like className={index >=…
Tobi
  • 363
  • 5
  • 15
3
votes
1 answer

Rerender only specific Child Components in JSX map function

I am mapping through an array, which returns JSX Components for each of the items in the array. During runtime I want to pass down values. If they match the value of the individual items, their individual component gets modified. I am trying to find…
Tobi
  • 363
  • 5
  • 15
3
votes
1 answer

Is possible to use next inside of map in perl?

I would like to parse only headers name in main.c: #include "foo.h" #include "bar.h" #include int add(int a,int b){ return a+b; } int sub(int a, int b){ return a-b; } int main(){ printf("%i\n",add(1,2)); } So my perl script looks…
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
3
votes
1 answer

Populate select (drop down) with data from API after .map using vanilla JS

I am stuck on a problem. I am using code that looks something like the following to populate a select with data but I am not getting the expected result. let data = await response.json(); let result = data.checks.map((check) => { return { …
Aib Syed
  • 3,118
  • 2
  • 19
  • 30