Last active
February 26, 2023 21:59
-
-
Save mourginakis/fd50972e58dfc121c730669e36255b70 to your computer and use it in GitHub Desktop.
good example of imperative vs functional programming
This file contains hidden or 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
#!/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