-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathlambda.py
More file actions
66 lines (32 loc) · 1012 Bytes
/
lambda.py
File metadata and controls
66 lines (32 loc) · 1012 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
58
59
60
61
62
63
64
65
66
from functools import reduce
def squared(num): return num * num
# lambda num : num * num
print(squared(2))
def add_two(num): return num + 2
# lambda num : num + 2
print(add_two(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
add_ten = funcBuilder(10)
add_twenty = funcBuilder(20)
print(add_ten(7))
print(add_twenty(7))
########################
numbers = [3, 7, 12, 18, 20, 21]
squared_nums = map(lambda num: num * num, numbers)
print(list(squared_nums))
###############################
odd_nums = filter(lambda num: num % 2 != 0, numbers)
print(list(odd_nums))
#############################
numbers = [1, 2, 3, 4, 5, 1]
total = reduce(lambda acc, curr: acc + curr, numbers, 10)
print(total)
print(sum(numbers, 10))
names = ['Dave Gray', 'Sara Ito', 'John Jacob Jingleheimerschmidt']
char_count = reduce(lambda acc, curr: acc + len(curr), names, 0)
print(char_count)