Skip to content

Instantly share code, notes, and snippets.

@mourginakis
Last active February 26, 2023 21:59
Show Gist options
  • Save mourginakis/fd50972e58dfc121c730669e36255b70 to your computer and use it in GitHub Desktop.
Save mourginakis/fd50972e58dfc121c730669e36255b70 to your computer and use it in GitHub Desktop.
good example of imperative vs functional programming
#!/usr/bin/env python3
from functools import reduce
import numpy as np
# mutable, imperative
def fact1(n):
out = 1
for i in range(1, n+1):
out *= i
return out
# functional, declarative
def fact2(n):
return reduce(np.multiply, range(1, n+1))
assert( fact1(8) == fact2(8) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment