Last active
June 8, 2021 17:25
-
-
Save terror/4d86aaf49cc724d0bfe5af11d05da88e to your computer and use it in GitHub Desktop.
Code for my `Higher order functions` blog post
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
def filter(func, iterable): | |
return [el for el in iterable if func(el)] | |
def map(func, iterable): | |
return [func(x) for x in iterable] | |
def reduce(func, iterable, init=None): | |
it = iter(iterable) | |
val = next(it) if init is None else init | |
for el in it: | |
val = func(val, el) | |
return val | |
def summation(func, N): | |
return sum(list(map(func, range(1, N)))) | |
def timer(func): | |
def wrap(*args, **kwargs): | |
start = time.perf_counter() | |
func(*args, **kwargs) | |
end = time.perf_counter() | |
print(f'Function {func.__name__} executed in {end - start:.4f}s') | |
return wrap | |
@timer | |
def waste(n): | |
for _ in range(n): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment