'Function that uses some of its variables only conditionally

Suppose f(x,y) is a bivariate function defined along the following lines. So f uses the second variable only conditionally.

def b
   .
   .
   .

def f(x,y):
   if b == True
      return (x,1)
   else
      return (x,y)

I want to rewrite f as a univariate function of x which at a later stage calls a function which takes y, but only if necessary!

So I want to curry and write something along the following lines.

def g(x):
   if b == True
      return (x,1)
   else
      return f(x,?)

Where f(x,?) is itself the function i_1x(y)=(x,y).

So I want to only inject the second variable conditionally. Evidently g returns two different types, which is likely a problem. We can resolve this artifially by having the first condition returns the function p_1(y)=(x,1), but that would return a function which would still ask for another input.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source