Understanding the Map Function

Understanding the map function

map isn't particularly pythonic. I would recommend using list comprehensions instead:

map(f, iterable)

is basically equivalent to:

[f(x) for x in iterable]

map on its own can't do a Cartesian product, because the length of its output list is always the same as its input list. You can trivially do a Cartesian product with a list comprehension though:

[(a, b) for a in iterable_a for b in iterable_b]

The syntax is a little confusing -- that's basically equivalent to:

result = []
for a in iterable_a:
for b in iterable_b:
result.append((a, b))

Understanding the concept of map() function using lambda expression

For each item in rdd it creates a tuple. For that rdd item (x), the second value of that rdd item (x[1]), is placed in the tuple being created along with the number 0.

I don't have pyspark installed, so I'm just using the built in map function to show how this transformation would work:

>>> rdd = ['ab', 'xyz', 'jk', 'pq']
>>> list(map(lambda x : (x[1],0), rdd))
[('b', 0), ('y', 0), ('k', 0), ('q', 0)]

Having trouble understanding the map() method

From MDN

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

The map() never change the original array but just create a new array according to the provided function.

In your code, the map() method creates a new array of Player components(the result of the provided callback).

Understanding the map function when the first argument uses flip

No, here the function map :: (a -> b) -> [a] -> [b] takes is (flip subtract 20), this is the parameter you pass to map. So that means that:

map (flip subtract 20) [1,2,3,4]

is equivalent to:

[flip subtract 20 1, flip subtract 20 2, flip subtract 20 3, flip subtract 20 4]

flip :: (a -> b -> c) -> b -> a -> c is a function that takes a function and flips the parameters. So flip subtract 20 is semantically equivalent to \x -> subtract x 20. Our list is thus equivalent to:

[subtract 1 20, subtract 2 20, subtract 3 20, subtract 4 20]

subtract :: Num a => a -> a -> a is the "flipped" version of (-), so it is equivalent to:

[20 - 1, 20 - 2, 20 - 3, 20 - 4]

and thus equivalent to:

Prelude> map (flip subtract 20) [1,2,3,4]
[19,18,17,16]

A shorter version of the above expression is thus:

map (20 -) [1,2,3,4]

What is the concept of Array.map?

Let's rewrite it a bit, and start working from inside out.

var mapCell = function (row) {
return columns.map(
function(column) {
return {
column : column,
value : getColumnCell(row, column)
}
}
)
}

The function(column) part is essentially a function that takes a column as a parameter, and returns a new object with two properties:

  • column, that is the original value of the parameter, and
  • value, that is the result of calling the getColumnCell function on the row (external variable) and column (parameter)

The columns.map() part calls the Array.map function, that takes an array and a function, and runs the function for every last item of it, and returns the results. i.e. if the input is the array [1, 2, 3, 4, 5] and the function is something like isEven, the result will be the array [false, true, false, true, false]. In your case, the input are the columns, and the output is a list of objects, each of which has a column and a value properties.

Lastly, the var mapCell = function (row) part declares that the variable mapCell will contain a function of one variable called row - and this is the same row that is used in the inner function.

In a single sentence, this line of code, declares a function that when run, will take a row and return values for all columns for that row.

Understanding the map function with function composition

Operators (defined as functions with no alphanumeric characters in their name, which are used in infix style) have lower precedence than other functions in Haskell. Therefore your expression:

map chr . map (1+) . map ord ['a', 'b', 'c', 'd', 'e']

is parsed as:

(map chr) . (map (1+)) . (map ord ['a', 'b', 'c', 'd', 'e'])

And this doesn't make any sense - the . operator combines 2 functions, and map ord ['a', 'b', 'c', 'd', 'e'] is not a function, but a list of numbers.

But there are still better alternatives to your correct version with all the nested parentheses. What you want to do is start with the list of characters, and successively map 3 functions over it. That, by definition of function composition, is the same as applying the composition of the 3 map s to it, ie:

(map chr . map (1+) . map ord) ['a', 'b', 'c', 'd', 'e']

which as @chi points out below, would often be written like this, using the function application operator $ (which has lower precedence than any other operator) to avoid the need for any parentheses:

map chr . map (1+) . map ord $ ['a', 'b', 'c', 'd', 'e']

And, because map f . map g is always the same as map (f . g) (this should be obvious when you stop to think about what these expressions mean - it's also the fundamental law the map operation must satisfy in order to make the list type into a Functor), this can be alternatively written as:

map (chr . (1+) . ord) ['a', 'b', 'c', 'd', 'e']

In my opinion this is the best and most readable version.

Although even better, as @chi again points out, is to use that chr . (1+) . ord is equivalent to succ, the built-in function for getting the "next" value of a type that can be enumerated. So you can just write map succ ['a', 'b', 'c', 'd', 'e']

How does the map() function in Processing work?

The map() function is a useful shortcut and you won't regret the time spent at understanding it.

This is its syntax:

variable2 = map(variable1, min1, max1, min2, max2);

The function establishes a proportion between two ranges of values:

min1 : min2 = max1 : max2

you can read it as: min1 is to min2 as max1 is to max2.

variable1 stores a value between the first range min1~max1.

variable2 gets a value between the second range min2~max2.

This is the equation the function solves for the programmer:

variable2 = min2+(max2-min2)*((variable1-min1)/(max1-min1))

This is the Java code behind the Processing map() function:

static public final float map(float value, 
float istart,
float istop,
float ostart,
float ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}

Haskell: Understanding the map function when used with lambda function

(- 5) 5 gives out an error because prefix minus is a special case in the language syntax: (- 5) means minus five, the number, and not a function that subtracts five (see also: Currying subtraction). That being so, I will focus on the (+) case, which is not exceptional.

In your second expression, map (\f x -> f 5 x) [(-),(+),(*)], the second element of the result list will be:

(\f x -> f 5 x) (+)

When evaluating such a thing by hand, it is important to be careful to not mix up prefix, infix and sectioned uses of operators. Application here gives out...

\x -> (+) 5 x  -- Prefix syntax (note the parentheses around the operator)

... which is equivalent to...

\x -> 5 + x  -- Infix syntax

... and to:

\x -> (5 +) x  -- Left section
\x -> (+ x) 5 -- Right section

(5 +) -- Left section, pointfree

So the sections, which are patterned after infix usage of the operators, should be the other way around relative to your question. As for map ($ 5) [(-),(+),(*)], it is equivalent to map (\f x -> f 5 x) [(-),(+),(*)], your second expression. You can confirm that by using the fact that ($) f x = f x to figure out what the ($ 5) right section is.



Related Topics



Leave a reply



Submit