-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps_functional_programming_demo.py
More file actions
72 lines (39 loc) · 1.38 KB
/
ps_functional_programming_demo.py
File metadata and controls
72 lines (39 loc) · 1.38 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
# funtional programming is passing functions as an argument to a function
# below hex,ord,sum are some functions
import re
print '\n\n'
print '1 ----------- with inbuilt functions ------------------------'
items = [2, 1, 3, 4, 5, 6, 7, 8]
temp = map(hex, items)
print temp
print
ascii = map(ord, 'peter pan')
print ascii
print
print map(sum, [[1,2,3], [5,6,7], [8,9,0]]) # for each element the sum method is called, so map loops internally
print '\n\n'
print '2 ------------- with user defined funtions ----------------------'
ascii = [112, 101, 116, 101, 32, 112, 97, 110]
def tag_it(av):
return 'ascii char="{}">{}</ascii>'.format(chr(av), av)
print map(tag_it, ascii)
print
print tag_it
print '\n\n'
print '3 ------------- using lambda ----------------------'
ascii = [112, 101, 116, 101, 32, 112, 97, 110]
print map(lambda av: 'ascii char="{}">{}</ascii>'.format(chr(av), av), ascii)
print
print tag_it
print '\n\n'
print '4 ------------- functional programmng using filter ----------------------'
items = range(1, 55)
print items
print
print filter(lambda n: n%7==0, items) # print multiples of 7
print '\n\n'
print '5 ------------- functional programmng using filter and lambda for file operations ----------------------'
pattern = 'root'
mlines =filter(lambda line: re.search(pattern, line, re.I), open('resources/passwd.txt'))
for line in mlines:
print line