Skip to content

Instantly share code, notes, and snippets.

@asukaminato0721
Created January 1, 2022 12:32
Show Gist options
  • Save asukaminato0721/b96c75067d620daac0c835dd8740227d to your computer and use it in GitHub Desktop.
Save asukaminato0721/b96c75067d620daac0c835dd8740227d to your computer and use it in GitHub Desktop.
make map and filter easy to use.
from functools import wraps
from typing import Callable
def wrapper(f: Callable):
@wraps(f)
def inner(*args):
if len(args) == 1 and isinstance(args[0], Callable):
def curry(x):
return f(*args, x)
return curry
else:
return f(*args)
return inner
map = wrapper(map)
filter = wrapper(filter)
print(*map(lambda x: x + 1)([1, 2, 3]))
print(*map(lambda x: x + 1, [3, 4, 5]))
print(*filter(lambda x: x % 2 == 0)([1, 2, 3, 4, 5]))
print(*filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment