-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHigher_Order_Functions.py
More file actions
161 lines (95 loc) · 3.25 KB
/
Copy pathHigher_Order_Functions.py
File metadata and controls
161 lines (95 loc) · 3.25 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
## Higher Order Functions ##
"""
In Python functions are treated as first class citizens, allowing you to perform the following operations on functions:
- A function can take one or more functions as parameters.
- A function can be returned as a result of another function.
- A function can be modified.
- A function can be assigned to a variable.
In this section, we will cover:
- Handling functions as parameters.
- Returning functions as return value from another functions.
- Using Python closures and decorators.
"""
# Function as a Parameter
def sum_one(value):
return value + 1
def sum_five(value):
return value + 5
def sum_two_values_and_add_value(first_value, second_value, f):
return f(first_value + second_value)
print(sum_two_values_and_add_value(5,2,sum_one))
print(sum_two_values_and_add_value(5,2,sum_five))
def sum_numbers(nums): # normal function
return sum(nums) # a sad function abusing the built-in sum function :<
def higher_order_function(f, lst): # function as a parameter
summation = f(lst)
return summation
result = higher_order_function(sum_numbers, [1, 2, 3, 4, 5])
print(result) # 15
# Function as a Return Value
def square(x): # a square function
return x ** 2
def cube(x): # a cube function
return x ** 3
def absolute(x): # an absolute value function
if x >= 0:
return x
else:
return -(x)
def higher_order_function(type): # a higher order function returning a function
if type == 'square':
return square
elif type == 'cube':
return cube
elif type == 'absolute':
return absolute
result = higher_order_function('square')
print(result(3)) # 9
result = higher_order_function('cube')
print(result(3)) # 27
result = higher_order_function('absolute')
print(result(-3)) # 3
# Python Closures
"""
Python allows a nested function to access the outer scope of the enclosing function.
This is is known as a Closure. Let us have a look at how closures work in Python.
In Python, closure is created by nesting a function inside another encapsulating function
and then returning the inner function.
"""
def add_ten(original_value):
val = 10
def add(num):
return num + val + original_value
return add
closure_result = add_ten(1)
print(closure_result(5)) # 15
print((add_ten(5))(1))
# Built-in Higher Order Functions
# Map
"""
La funcion map necesita un elemento iterable (listas,tuplas...)
Syntax
map(function, iterables)
"""
def multiply_two(number):
return number * 2
number = [2, 5, 10, 21, 30]
print(list(map(multiply_two ,number)))
print(list(map(lambda number: number * 2 ,number)))
# Filter
def fil_greater_than_ten(number):
if number > 10:
return True
return False
print(list(filter(fil_greater_than_ten, number)))
print(list(filter(lambda number: number > 10 ,number)))
# Reduce
from functools import reduce
def sum_two_values(first_value, second_value):
return first_value + second_value
print(reduce(sum_two_values, number))
"""
la funcion "reduce" reduce todos los elementos del iterable a un valor unico, es similar a una sumatora.
"""
word = ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
print(reduce(sum_two_values, word))