-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusing_functional.py
More file actions
27 lines (23 loc) · 981 Bytes
/
Copy pathusing_functional.py
File metadata and controls
27 lines (23 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# recently functional programming has returned in fashion
# a pure function has no side effects
# i.e. for given inputs the output is entirely predictable
# we need the 'reduce' capabilites of the functools library
from functools import reduce
def square(x):
return x*x
def add(x,y): # we will use this to recursively 'reduce' a bunch of numbers
return x+y
# in order to use 'filter' we must return True or False
def isOdd(x): # assume it's an int
return x%2 != 0 # this function returns True or False
if __name__ == '__main__':
# here we can exercise this module
# let's make a list of square numbers
sq_l= list( map( square, range(1,6) ) ) # take the 'square' fn and apply a bunch of numbers to it
print(sq_l)
# we can use a function to filter values
odds_l = list( filter( isOdd, range(-10**8, 10**8) ) )
# print(odds_l)
# using 'reduce'
r = reduce( add, odds_l, 10 ) # also add ten!!
print(r)