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

Backpropagating gradients through nested tf.map_fn

I would like to map a TensorFlow function on each vector corresponding to the depth channel of every pixel in a matrix with dimension [batch_size, H, W, n_channels]. In other words, for every image of size H x W that I have in the batch: I extract…
gab
  • 792
  • 1
  • 10
  • 36
5
votes
2 answers

Using map in Haskell

I want to use map to substract the average of a list from each element of a list using map. So far I got this function for calculating the average: averageOfList :: [Float] -> Float averageOfList [] = 0 averageOfList a = ((foldl (+) 0 a) /…
Hello there
  • 51
  • 1
  • 1
  • 4
5
votes
1 answer

How to add a text prefix to an array on Swift?

Right now I have a array that when printed just displays what number I submitted. I would like the word "car" to be in front of every array number. For example: I enter 1 and 2 in the array. When the array is called it would look like [car 1, car 2]…
user7961001
5
votes
2 answers

Understanding Python map function + range

I'm trying to understand the difference between these lines of code: list(''.join(map(lambda x: str(x * 3), range(1, 4)))) Out: ['3', '6', '9'] as expected. However: list(''.join(map(lambda x: str(x * 5), range(1, 4)))) outputs ['5', '1', '0',…
Pierre B
  • 654
  • 2
  • 6
  • 15
5
votes
3 answers

Convert a nested for loop to a map equivalent

Example: for x in iterable1: expression The map form would be: map(lambda x: expression, iterable1) How do I extend this to a nested for loop using only map and without list comprehensions? Example: for x in itr1: for y in itr2: …
lapin
  • 432
  • 1
  • 5
  • 11
5
votes
1 answer

Calling none in maps in Python 3

I am doing the following in Python2.7: >>> a = [1,2,3,4,5] >>> b = [2,1,3,4] >>> c = [3,4] >>> map(None, a, b, c) [(1, 2, 3), (2, 1, 4), (3, 3, None), (4, 4, None), (5, None, None)] I am trying to do something similar in Python3 >>> a =…
Echchama Nayak
  • 971
  • 3
  • 23
  • 44
5
votes
3 answers

Unpack nested list for arguments to map()

I'm sure there's a way of doing this, but I haven't been able to find it. Say I have: foo = [ [1, 2], [3, 4], [5, 6] ] def add(num1, num2): return num1 + num2 Then how can I use map(add, foo) such that it passes num1=1, num2=2 for…
binaryfunt
  • 6,401
  • 5
  • 37
  • 59
5
votes
6 answers

Partial application of functions and currying, how to make a better code instead of a lot of maps?

I am a beginner at Haskell and I am trying to grasp it. I am having the following problem: I have a function that gets 5 parameters, lets say f x y w z a = x - y - w - z - a And I would like to apply it while only changing the variable x from 1 to…
5
votes
6 answers

Java - Spark SQL DataFrame map function is not working

In Spark SQL when I tried to use map function on DataFrame then I am getting below error. The method map(Function1, ClassTag) in the type DataFrame is not applicable for the arguments (new Function(){}) I am following spark 1.3 documentation as…
user3206330
  • 51
  • 1
  • 1
  • 2
5
votes
2 answers

Implementation of variadic map function in Scheme

As you can see in the example below, map function in Scheme is variadic function. > (map (lambda (number1 number2) (+ number1 number2)) '(1 2 3 4) '(10 100 1000 10000)) '(11 102 1003 10004) I want to implement this variadic option, but I…
Nir
  • 1,882
  • 4
  • 26
  • 44
5
votes
3 answers

Not return anything from LISP/Scheme

Basically, I would like to use map to do selection in a list like (define tbl '(a b c d)) (map (lambda (item 'c) (if (eq? item 'c) item (...what in else?) ))) The result I want is '(c) I tried leave the else part empty, it complains that the else…
Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59
4
votes
3 answers

Haskell: Map function with tuples

I have to write a Haskell program that does the following: Main> dotProduct [(1,3),(2,5),(3,3)] 2 [(2,3),(4,5),(6,3)] I have to do it both with and without map function. I already did it without map, but I have no clue to do it with map. My…
WKQ
  • 53
  • 1
  • 5
4
votes
3 answers

How to map over async generators?

Let's say we have an async generator: exports.asyncGen = async function* (items) { for (const item of items) { const result = await someAsyncFunc(item) yield result; } } is it possible to map over this generator? Essentially I want to…
4
votes
3 answers

How does mapM work with const functions in Haskell?

as I had been looking for ways to optimize a password cracker I had been making, I came across a much shorter implementation of all possible character combinations in a list, which used this function: mapM (const xs) [1..n] where xs could be the…
Atyafi
  • 41
  • 2
4
votes
3 answers

Python3: Does the built-in function "map" have a bug?

The following I had with Python 3.8.1 (on macOS Mojave, 10.14.6, as well as Python 3.7 (or some older) on some other platforms). I'm new to computing and don't know how to request an improvement of a language, but I think I've found a strange…