Skip to content

Instantly share code, notes, and snippets.

@naiquevin
Created November 7, 2015 15:07
Show Gist options
  • Save naiquevin/cdbb8401c170ba1a7c7d to your computer and use it in GitHub Desktop.
Save naiquevin/cdbb8401c170ba1a7c7d to your computer and use it in GitHub Desktop.
fnil's cousin from python land
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