Skip to content

Instantly share code, notes, and snippets.

Last active June 8, 2021 17:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Code for my `Higher order functions` blog post
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
def waste(n):
for _ in range(n):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment