-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_functions.py
30 lines (21 loc) · 945 Bytes
/
lambda_functions.py
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
# A lambda function is an anonymous function.
# It's speciality is that it is a single line function and is often passed to other functions
def square(n):
return n * n
print(square(4))
# Rewriting the above using lambda.
f = lambda n: n * n
print(f(4))
# The beauty of this comes when you use them in other functions
nums = [2, 4, 7, 12, 59, 8, 3, 65, 7]
# The inbuilt function filter can be used to filter an iterable input based on a function.
# The input function can be a complex function. If the work is straightforward, you can deploy a lambda function there.
evens = list(filter(lambda n: n % 2 == 0, nums))
print(evens)
# The inbuilt function map() is used to apply a some action to all inputs of an iterable
squares = list(map(lambda n: n*n ,evens))
print(squares)
# Finally, the reduce() function is used to reduce the data to a single value
from functools import reduce
sum = reduce(lambda a,b : a+b, squares)
print(sum)