-3
def compose(f,g): 

  return lambda f: f + 1
  return lambda g: g

how can I specify the order of the return statements

These are the test cases;

add1 = lambda a: a+1
this   = lambda a: a

test.expect( compose(add1,this)(0) == 1 )
Conor
  • 494
  • 6
  • 15
  • 1
    There is no error. It will return the first lambda which increments its input by 1. What exactly do you want to achieve? – prashkr Jun 23 '16 at 11:39
  • the requirement is return 2 functions, it fails tests where order is specified – Conor Jun 23 '16 at 11:41
  • 2
    How about `return (lambda f: f + 1, lambda g: g)` and access using their corresponding indexes? – prashkr Jun 23 '16 at 11:44

3 Answers3

2
def compose(f1, g1):
    # do something
    return lambda f:f+1, lambda g:g

will return a tuple of two functions.

You can get the individual functions like this:

func1, func2 = compose(a,b)
SvbZ3r0
  • 638
  • 4
  • 19
1

You cannot have two return statements in a function. Once the first one is called, the second will not be reached because the function has already returned. You could, however, use a tuple to organize the output, which would look like this.

def compose(f,g):

    return (lambda f: f + 1, lambda g: g)

Be careful though, because this will return the actual lambdas as the below example shows:

In [7]: def func(f, g):                                                                                                                                                 
   ...:     return (lambda f: f + 1, lambda g: g)                                                                                                                       
   ...:                                                                                                                                                                 

In [8]: func(0, 0)                                                                                                                                                   
Out[8]: (<function __main__.<lambda>>, <function __main__.<lambda>>)

Notice the types shows by line out[8]. This means that what ever variables that the function returns to will be the actual functions, not a number. To return a number, don't use lambdas, just calculate the numbers normally.

It's also worth noting, that the parameters have no effect on this function.

rtmh
  • 439
  • 3
  • 9
  • tried this solution: "Traceback: in TypeError: 'tuple' object is not callable" – Conor Jun 23 '16 at 12:25
  • its a codewar kata so I dont have access to the solution or any other error details – Conor Jun 23 '16 at 12:30
  • That is because you must specify which element to access first. The tuple is not a function but it contains the functions. Like arrays, use the [] operator to chose which function to use. This is explained at the link I provided as well. – rtmh Jun 23 '16 at 12:31
  • @Conor I think that solving problems for Codewar is outside the scope of Stack Overflow. Perhaps study the terminology they use and what exactly that means in python and code in general for more insight. – rtmh Jun 23 '16 at 12:34
0
def compose(f,g): 

   return lambda f: f + 1,lambda g: g

print compose(f,g)[0]
print compose(f,g)[1] 
khelili miliana
  • 3,730
  • 2
  • 15
  • 28