Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 inputs to a function?

So Ive been giving the following code in a kind of sort of python class. Its really a discrete math class but he uses python to demonstrate everything. This code is supposed to demonstate a multiplexer and building a xor gate with it.

def mux41(i0,i1,i2,i3):
    return lambda s1,s0:{(0,0):i0,(0,1):i1,(1,0):i2,(1,1):i3}[(s1,s0)]

def xor2(a,b):
    return mux41(0,1,1,0)(a,b)

In the xor2 function I dont understand the syntax behind return mux41(0,1,1,0)(a,b) the 1's and 0's are the input to the mux function, but what is the (a,b) doing?

like image 847
Deekor Avatar asked Feb 01 '13 23:02

Deekor


1 Answers

The (a, b) is actually the input to the lambda function that you return in the mux41 function.

Your mux41 function returns a lambda function which looks like it returns a value in a dictionary based on the input to the mux41 function. You need the second input to say which value you want to return.

It is directly equivalent to:

def xor2(a,b):
    f = mux41(0,1,1,0)
    return f(a,b)
like image 113
Volatility Avatar answered Sep 30 '22 18:09

Volatility