-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional_programming.py
More file actions
58 lines (44 loc) · 961 Bytes
/
Copy pathfunctional_programming.py
File metadata and controls
58 lines (44 loc) · 961 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# if statements
# procedural
def a(i):
if i==1:
return "one"
elif i==2:
return "two"
else:
return "three"
print(a(1)) # one
print(a(2)) # two
print(a(3)) # three
#functional
a = lambda x: x
b = lambda x: (x == 1 and a("one"))\
or (x == 2 and a("two"))\
or (a("three"))
print(b(1)) # one
print(b(2)) # two
print(b(3)) # three
print("===== grocery list =====")
# procedural
print("*** procedural ***")
grocery_list = ['apples','bananas','oranges','milk']
for grocery in grocery_list:
print(grocery)
#functional
print("*** functional ***")
grocery_list = ['apples','bananas','oranges','milk']
def grocery(list):
print(list)
map(grocery, grocery_list)
#print(result)
#print(list(result))
#grocery_list)
# Python program to demonstrate working
# of map.
# Return double of n
def addition(n):
return n + n
# We double all numbers using map()
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))