Lambda function syntax

I am well at the beginning of the study of the Lambda function and I am well lost. I know the code could only be done with the " sum " function but I want to learn 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 take four numbers entered and show the sum and I think the error is in line 5. How would that line look to sum all the numbers stored in the list?

Author: Lucas Souza, 2018-02-19

1 answers

lambda in Python it 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 created a new function, and for short we call the function created using the lambda of "lambda function"

In this case, the line d = (lambda x, y: x + y, a) actually does not even create only the lambda function - as the precedence of the "," is less than that of the lambda (the comma is resolved after the lambda is already set), this expression actually creates a tuple, where the first item will be the lambda function lambda x, y: x + y and the second item will be a.

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

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

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, the", " terminates the lambda - but in this case, it serves to separate the lambda from the next argument to function reduce. And the 0 is the value used for the x in the first function call, which is called once for each element in a, which will always be assigned to y. Functions defined with lambda do not need a return: the result of the only expression they already contain is their return value - and what reduce does is put that value as x on the next call - until the elements of "a" are finished-the last value that the lambda function returns is returned by the reduce.

To be clear, note that using lambda is exactly the same thing as creating a normal function, with def and using the name of that function where you use lambda; that is, this code could also be:

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

d = reduce(soma2, a, 0)

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

Reduce by extension could be something like:

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

But notice that for this simple case, you don't really gain much from calling the function soma2-on the contrary, it loses readability. So the most normal will be you VOC write:

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

There is still a small performance gain by not calling 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 need to pass functions as parameters (some functional languages, in the extreme, don't even have an equivalent to for - you have to use functions as a function parameter even to emulate a loop, as in haskell or Scheme). Be very cool as an exercise, and understand different ways of approaching problems, but in practice require much 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 better expressiveness than map and filter using list comprehensions and generator expressions ( e.g. [x * 2 for x in range(10)]) - but reduce only exists as function same. If you want to explore this paradigm further, there's a really cool project on top of Python, called "Coconut". It creates a new language that is a superset of Python (and in fact, 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 suitable for Functional Programming:

Http://coconut-lang.org /

 3
Author: jsbueno, 2020-09-16 14:45:03