Created
November 7, 2015 15:07
-
-
Save naiquevin/cdbb8401c170ba1a7c7d to your computer and use it in GitHub Desktop.
fnil's cousin from python land
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import izip_longest | |
def fnone(f, *xs): | |
"""Takes a function f, and returns a function that calls f, replacing | |
a None first argument to f with the supplied value x. Higher arity | |
versions can replace arguments in the second and third | |
positions (y, z). Note that the function f can take any number of | |
arguments, not just the one(s) being None-patched. | |
""" | |
def g(*args): | |
ys = [a if a is not None else b | |
for a, b in izip_longest(args, xs)] | |
print(ys) | |
return f(*ys) | |
return g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment