-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
67 lines (36 loc) · 1.17 KB
/
Copy pathlambda.py
File metadata and controls
67 lines (36 loc) · 1.17 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
def squared(num): return num * num
# lambda num : num * num
print(squared(2))
def addTwo(num): return num + 2
# lambda num : num + 2
print(addTwo(12))
def sum_total(a, b): return a + b
# lambda a, b : a + b
print(sum_total(10,8))
#########################
def funcBuilder(x):
return lambda num : num + x
addTen = funcBuilder(10)
addTwenty = funcBuilder(20)
print(addTen(7))
print(addTwenty(7))
########################
# Higher Order Functions
numbers = [3,7,12,18,20,21]
squared_nums = map(lambda num : num * num, numbers)
# numbers is the second argument, a data collection
print(list(squared_nums))
#########################
odd_nums = filter(lambda num : num % 2 != 0, numbers)
print(list(odd_nums))
##############################
from functools import reduce
lambda acc, curr: acc + curr
numbers = [1,2,3,4,5,1]
total = reduce(lambda acc, curr: acc + curr, numbers, 10)
print(total)
print(sum(numbers, 10))
lambda acc, curr : acc + len(curr)
name = ['Dave Gray', 'Sara Ito', 'John Jacog Jingleheimerschmidt']
char_count = reduce(lambda acc, curr : acc + len(curr), name, 0)
print(char_count)