+ 1
Can some one explain me a Lambda function below code,i can't understand how a is used as myfunc(2) ?
def myfunc(n): return lambda a : a * n mydoubler = myfunc(2) print(mydoubler(11))
9 Respostas
+ 6
Have you seen the movie Inception? This is similar.
Your typical function returns a *value* (that may be calculated based on a parameter).
def twice(n):
return 2 * n
Now if you want to abstract away, how many times you want to multiply something, you can end up with a function that you posted. It only takes one argument (n) but to actually perform the operation, it needs also another argument (a).
So that function, instead of returning a value, it returns a *thing* which *creates* a value. In other words, it returns another function (that is still incomplete because it's missing one argument). This is also called a closure.
You can also use this like so:
result = myfunc(2)(11)
which will result in 22
+ 5
# this function return reference to lambda expression with passed parameter n, still 'a' is unknown..
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2) # this will get a lambda function returned as
lambda a : a * 2
print(mydoubler(11)) # this will call lambda expression with 11 so output returned is 11*2
Hope it helps...
+ 4
Jeya Prakash J
Compare these two lines
return 2 * n
return lambda a: a * n
In the second one, the expression returned, is a function which takes one argument (a).
When you invoke this function with the parentheses:
mydoubler(11)
Then the value 11 is assigned and the function is executed.
+ 3
Tibor Santa I'm trying to understand when this would be useful. I have some ideas but none, it seems to me, couldn't be done without a simple assignment or a composite function. But I think it has its uses because you'd have said so otherwise.
Would you mind creating a simple scenario off the top of your head where only this could work or works best? (grammar was too bad, hope it's clearer now)
Thanks for the question, btw, Jeya Prakash Jr.
Also: I'm asking about it here because it seems related. I could move it but it's hard to tell for me.
+ 3
Tibor Santa Thank you for the response. It explains many things.
A feeling of lucky bonus on my end.
About the last paragraph:
If in the func(edited to remove the word implicit), one of the args depended on two other independent args, I take it, this could be of use, especially if the domain for the function is large? Or not? (just putting it out there if you feel like answering that too)
I really don't have any idea if this is considered a tangent or even a digression, apologies in advance.
Edit: Saw your other post with the code after sending mine, thank you đ
+ 2
Korkunç el Gato
Currying is a concept known in functional programming, where a function taking multiple arguments, can always be expressed as a chain of functions, each taking only a single argument. This is exactly happening here.
f(a, b, c) = g(a)(b)(c)
This doesn't look terribly useful, but this simple insight allowed the creation of functional languages such as Haskell.
(By the way, both this approach and the language were named after the mathematician Haskell Curry).
A similar concept is also partial function application, where a multi-arg function's arity (number of arguments taken) is reduced to 1, by fixing all other arguments to a constant.
If you look for practical use, imagine you have a complicated math formula with multiple unknowns. You can substitute all but one value and proceed with some time-consuming calculations, until you reach a point where only a single value is required. Then you can return this as a function, and reuse the difficult calculation.
+ 2
In some ways this feels very similar to inheritance in OOP. Where parent class has certain attributes, and we only want to override the child class by adding one more field.
Thinking about functions which only require a single argument and never more, consider "map". It performs a transformation over a collection, taking each element as a single argument of the transform function. So circling back to the original question, things like this can be expressed with map easily, making this abstraction worthwhile and very well readable:
doubles = map(mydoubler, range(10))
triples = map(mytripler, range(10))
Otherwise you would have to write something like this:
doubles = map(lambda x: x*2, range(10))
Which is still not terrible, but if the function itself is complicated or there are more "partial" variations, it can pay off.
https://code.sololearn.com/cBZ8CthiaJ8h/?ref=app
+ 2
Korkunç el Gato no problem, this is very much related to this question and finally I found a good use for showing off my old program đ€Ł
Anyhow, currying and partial function application are key building blocks of functional languages, but even in Python and other mainstream languages we can benefit from knowing and applying these techniques.
+ 1
Tibor Santa If myfunc returns the incomplete function,which is a*2.
How Mydoubler value 11 exactly assigned to the value a?