2

i have to convert the following functions into lambda functions, without changing the semantics. How i have to do this?

u = 1

def method1(x):
    def method2(n):
        def method3(m):
            return x + n + m
        def method4():
            global u
            u += 1
        method4()
        return method3
    def method5(y):
        return x + y
    return method2, method5

Edit: Here are my attempts:

method1 x: method2 n: method3 m: x+n+m, method 4 (): method 3, method5 y: x+y, method2 method3

Edit2:

method1 = lambda x:
    method2 = lambda n:
        method3 = lambda m:
         x + n + m
        method4 = lambda :
         global u
         u = u + 1
        #dont know how to integrate "method4() return method3"
    method5 = lambda y:
      x + y
    method2, method5
19
  • 1
    1) read about lambdas. Commented Jan 21, 2017 at 15:13
  • 1
    Looks like you aren't really comfortable with lambda syntax... Commented Jan 21, 2017 at 15:29
  • 1
    These are not lambda expressions. Please return to the first point: 1) read about lambdas. Commented Jan 21, 2017 at 15:29
  • 1
    method4 if not possible to transform to lambdas. And why you want to do this? Commented Jan 21, 2017 at 16:04
  • 1
    Basically, you've not covered point 1 enough. Lambdas cannot be multiple lines, nor do I think you can define them within one another Commented Jan 21, 2017 at 16:56

1 Answer 1

6

Python's lambdas are a lot more powerful than most people seem to think.

u = 1
(lambda x: (
    (lambda n: [
        (lambda m: x + n + m),
        (lambda: globals().__setitem__('u', globals()['u'] + 1))()
     ][0]),
    (lambda y: x + y)
))
Sign up to request clarification or add additional context in comments.

1 Comment

Never seen this before. Impressive writing. Validated your code with python 3.6.7 with much success.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.