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

I wanted to learn about usage mapin python, and a google search led me to http://www.bogotobogo.com/python/python_fncs_map_filter_reduce.php , which I found useful.

One of the codes on this page uses the for loop and puts mapit into the loop in an interesting way, and the list used in the function mapactually takes a list of two functions. Here is the code:

def square(x): 
    return (x**2)

def cube(x):
    return (x**3)

funcs = [square, cube]

for r in range(5):
    value = map(lambda x: x(r), funcs)
    print value

output:

[0, 0]
[1, 1]
[4, 8]
[9, 27]
[16, 64]

So, at this point in this tutorial, I thought, “well, if you can write this code with the function on the fly (lambda), then you could write it using a standard function with def.” So I changed the code to this:

def square(x): 
    return (x**2)

def cube(x):
    return (x**3)

def test(x):
    return x(r)

funcs = [square, cube]

for r in range(5):
    value = map(test, funcs)
    print value

, , , r . , . :

def square(x): 
    return (x**2)

def cube(x):
    return (x**3)

def power(x):
    return x(r)

def main():
    funcs = [square, cube]
    for r in range(5):
        value = map(power, funcs)
        print value

if __name__ == "__main__":
    main()

, def power(x). , lambda x funcs.

, def, , ? python, , , .

+4
3

power() main():

def main():
    def power(x):
        return x(r)

    funcs = [square, cube]
    for r in range(5):
        value = map(power, funcs)
        print value

r , . .

lambda - r power():

def power(r, x):
    return x(r)

def main():
    funcs = [square, cube]
    for r in range(5):
        value = map(lambda x: power(r, x), funcs)
        print value

r - , !

lambda r, :

def power(r, x):
    return x(r)

def main():
    funcs = [square, cube]
    for r in range(5):
        value = map(lambda x, r=r: power(r, x), funcs)
        print value

r , . map() .

+4

Currying - . , , , , :

def square(x):
    return (x**2)

def cube(x):
    return (x**3)

def power(r):
    return lambda(x): x(r) # This is where we construct our curried function

def main():
    funcs = [square, cube]
    for y in range(5):
        value = map(power(y), funcs) # Here, we apply the first function
                                     # to get at the second function (which
                                     # was constructed with the lambda above).
        print value

if __name__ == "__main__":
    main()

, (a, b) -> c (, a b c) a -> (b -> c).

, , . , a -> b b a (a, b) a * b. "", " " - . ,

c (a * b)= (c b) a

, ,

(a, b) -> c  ~=  a -> (b -> c)
+3

power() itertools.product (value, func) ?

from itertools import product

# ...

def power((value, func)):
    return func(value)

for r in range(5):
    values = map(power, product([r], funcs))
    print values

, /, , , :

values = map(power, product(range(5), funcs))
print values

. power((value, func)) power(), 2 , value func.

def power(arg):
    value, func = arg
+1
source

All Articles