Created
January 1, 2022 12:32
-
-
Save asukaminato0721/b96c75067d620daac0c835dd8740227d to your computer and use it in GitHub Desktop.
make map and filter easy to use.
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 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