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 )
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 )
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)
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.
def compose(f,g):
return lambda f: f + 1,lambda g: g
print compose(f,g)[0]
print compose(f,g)[1]