Syntax of the Lambda function

0

I'm pretty early in the Lambda function study and I'm pretty lost. I know the code could only be done with the "sum" function but I want to learn the Lambda syntax.

a = []
for b in range(0, 4):
    c = int(input())
    a.append(c)
d = (lambda x, y: x + y, a)
print(d)

The program has to pick up four numbers typed and show the sum and I think the error is on line 5. How would this line add up to all the numbers stored in the list?

    
asked by anonymous 19.02.2018 / 04:36

1 answer

2

lambda in Python is not a function. It is a keyword that allows you to define a function as an expression, or part of an expression. When you use lambda, we say that you have created a new function, and to shorten it we call the created function using the lambda lambda function

In this case, the line d = (lambda x, y: x + y, a) does not actually create only the lambda function - since the precedence of "," is greater than lambda, this expression actually creates a tuple, where the first item is the function lambda lambda x, y: x + y and the second item will be a .

But from what you can tell from your program, it's not the lambda that interests you - you want to apply the function that adds x and y to all elements of a - what it does this is the reduce function.

The function reduces yes, it receives three parameters: A function in the first parameter, a sequence in the second, and an initial value in the third parameter. The function passed as the first parameter will always receive two arguments: the first that is the "accumulated result" and the second that is the "next item in the sequence". Thus, it is very common to use a lambda construct to create the function that goes in the first parameter of the call to reduce.

TL;DR: What you want in this program is this:

from functools import reduce
...
d = reduce(lambda x, y: x + y, a, 0)

What does it do? As in the case where you used it, the "," terminates the lambda - but in this case, it serves to separate the lambda from the next argument to the reduce function. And 0 is the value used for x on the first call to the function, which is called once for each element in a , which will always be assigned to y . The functions defined with lambda do not need a return : the result of the only expression they contain is already their return value - and what reduces it does is to put that value as x on the next call - until they are done the elements of "a" - the last value that the lambda function returns is returned by reduce .

To be very clear, realize that using lambda is exactly the same as creating a normal function, with def and using the name of that function where lambda is used; That is, this code could also be:

...
def soma2(x, y):
    return x + y

d = reduce(soma2, a, 0)

In short: both lambda and lambda are concepts that can be confusing if it is not absolutely clear to the programmer. If this is the case, it is best to avoid them and exchange them for the equivalent "long" code, which is much simpler to understand, in general, for both the writer and the reader. There are more situations where "lambda" is used in the real world - sometimes we need a function to actually do something very simple - to create a new list-type object when creating a "defaultdict", or a calculation for the value key in a sort when you call the "sorted". Since reduce is somewhat more restricted to "map-reduce" type jobs - if it's something simple, it's worth splitting into three lines for readability. If it is very computationally intensive, it pays to use a specialized map-reduce library, such as the hadoop tools or the pandas. So much so that in the transition to Python 3, the reduce is no longer a builtin and has to be imported from the functools module.

Reducing at length could be something like:

d = 0
for elemento in a:
   d = soma2(d, elemento)

But realize that for this simple case, you really do not gain much from calling the soma2 function - on the contrary, it loses readability. So the more normal it will be for you to write:

d = 0
for elemento in a:
    d += elemento

There is still a small gain in performance because it is not called an intermediate function. The idea of reduce is more for languages with more emphasis on the functional (or purely functional) programming paradigm, where it is more normal to pass functions as parameters (some functional languages, in the extreme, do not even have one equivalent to for you have to use functions as a parameter of functions including to emulate a loop, as in haskell or Scheme). They are cool as exercise, and understand different ways of approaching problems, but in practice they require far more expenditure of "mental energy" than a simple interactive loop, as above.

Having said all this: lambda, map, filter and reduce are a small fraction of what exists in the "functional" programming paradigm. Python allows for better expressiveness than map and filter using list comprehensions and generator expressions (ex. [x * 2 for x in range(10)] ) - but reduce only exists as a function itself. If you want to explore this paradigm further, there is a really cool project on top of Python, called "Coconut". It creates a new language that is a superset of Python (and in fact, the coconut files are compiled for Python and run with the normal Python runtime). Coconut defines dozens of tools and functions, plus an extended syntax, most appropriate for functional programming:

link

    
19.02.2018 / 04:49