Skip to content

Commit c17bba7

Browse files
authored
Merge pull request mouredev#4547 from thegera4/main
#21 - Go
2 parents db07487 + 002c235 commit c17bba7

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// NOTA: Un callback es una función que se pasa como argumento a otra función.
9+
10+
// Definimos un tipo para la funcion callback, en este caso recibe un entero y devuelve un entero
11+
type Callback func(int) int
12+
13+
// Definimos una funcion que aplique una operacion al numero que recibe y devuelve el resultado
14+
func applyCallback(num int, callback Callback) int {
15+
return callback(num)
16+
}
17+
18+
func main() {
19+
// Definimos una funcion que multiplica por 2 el numero.
20+
duplica := func(num int) int {
21+
return num * 2
22+
}
23+
24+
// Definimos otra funcion que suma el numero con 10
25+
suma := func(num int) int {
26+
return num + 10
27+
}
28+
29+
// Aplicamos la funcion duplicar al numero 5
30+
fmt.Println("Resultado de multiplicar 5 por 2:", applyCallback(5, duplica))
31+
32+
// Aplicamos la funcion triplicar al numero 5
33+
fmt.Println("Resultado de sumar 15 con 10:", applyCallback(15, suma))
34+
35+
// Extra
36+
simuladorPedidos()
37+
38+
}
39+
40+
func simuladorPedidos() {
41+
42+
// Definimos las funciones de confirmacion, listo y entrega
43+
confirmacion := func(num int) int {
44+
fmt.Println("Pedido confirmado")
45+
return num
46+
}
47+
48+
listo := func(num int) int {
49+
fmt.Println("Plato listo")
50+
return num
51+
}
52+
53+
entrega := func(num int) int {
54+
fmt.Println("Pedido entregado")
55+
return num
56+
}
57+
58+
// Simulamos un pedido de pizza
59+
procesarPedido("Pizza", confirmacion, listo, entrega)
60+
61+
// Simulamos un pedido de hamburguesa
62+
procesarPedido("Hamburguesa", confirmacion, listo, entrega)
63+
}
64+
65+
func procesarPedido(plato string, confirmacion, listo, entrega Callback) {
66+
fmt.Println("Procesando pedido de", plato)
67+
68+
time.Sleep(time.Duration(1+time.Now().Nanosecond()%10) * time.Second) // Simulamos un tiempo aleatorio entre 1 y 10 segundos
69+
confirmacion(1)
70+
71+
time.Sleep(time.Duration(1+time.Now().Nanosecond()%10) * time.Second)
72+
listo(2)
73+
74+
time.Sleep(time.Duration(1+time.Now().Nanosecond()%10) * time.Second)
75+
entrega(3)
76+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
"sort"
7+
)
8+
9+
// Nota: una funcion de orden superior (HOF) es una funcion que recibe como parametro
10+
// otra(s) funcion(es) o devuelve una funcion.
11+
12+
// Primero definimos una funcion de orden superior (recibe una funcion como parametro).
13+
func applyOperation(x int, y int, operation func(int, int) int) int {
14+
return operation(x, y)
15+
}
16+
17+
// Luego definimos dos funciones que seran pasadas como parametro a la funcion de orden superior anterior.
18+
func add(x int, y int) int {
19+
return x + y
20+
}
21+
22+
func subtract(x int, y int) int {
23+
return x - y
24+
}
25+
26+
// Y una funcion que regresa una funcion (tambien es una funcion de orden superior)...
27+
func makeMultiplier(factor int) func(int) int {
28+
return func(x int) int {
29+
return x * factor
30+
}
31+
}
32+
33+
func main() {
34+
fmt.Println("Resultado de sumar 5 y 3:", applyOperation(5, 3, add))
35+
fmt.Println("Resultado de multiplicar 5 y 3:", applyOperation(5, 3, subtract))
36+
37+
triple := makeMultiplier(3)
38+
fmt.Println("Resultado de multiplicar 8 por 3:", triple(8))
39+
40+
//Extra
41+
analisisEstudiantes()
42+
}
43+
44+
// Extra
45+
46+
// Definimos una estructura para los estudiantes
47+
type Student struct {
48+
name string
49+
birthDate string
50+
grades []float64
51+
}
52+
53+
// Definimos una funcion de orden superior que realiza el analisis de los estudiantes
54+
func applyAnalysis(students []Student, analysisType func([]Student) []string) []string {
55+
return analysisType(students)
56+
}
57+
58+
// Definimos las funciones para los tipos de analisis
59+
func average (students []Student) []string {
60+
var result []string
61+
for _, student := range students {
62+
sum := 0.0
63+
for _, grade := range student.grades {
64+
sum += grade
65+
}
66+
average := sum / float64(len(student.grades))
67+
result = append(result, student.name + ": " + fmt.Sprintf("%.2f", average))
68+
}
69+
return result
70+
}
71+
72+
func bestStudents (students []Student) []string {
73+
var result []string
74+
for _, student := range students {
75+
sum := 0.0
76+
for _, grade := range student.grades {
77+
sum += grade
78+
}
79+
average := sum / float64(len(student.grades))
80+
if average >= 9.0 {
81+
result = append(result, student.name)
82+
}
83+
}
84+
return result
85+
}
86+
87+
func birthDateSort (students []Student) []string {
88+
var result []string
89+
sort.Slice(students, func(i, j int) bool {
90+
date1, _ := time.Parse("2006-01-02", students[i].birthDate)
91+
date2, _ := time.Parse("2006-01-02", students[j].birthDate)
92+
return date1.After(date2)
93+
})
94+
for _, student := range students {
95+
result = append(result, student.name)
96+
}
97+
return result
98+
}
99+
100+
func highestGrade (students []Student) []string {
101+
var result []string
102+
highest := 0.0
103+
for _, student := range students {
104+
for _, grade := range student.grades {
105+
if grade > highest {
106+
highest = grade
107+
}
108+
}
109+
}
110+
for _, student := range students {
111+
for _, grade := range student.grades {
112+
if grade == highest {
113+
result = append(result, student.name + ": " + fmt.Sprintf("%.2f", grade))
114+
}
115+
}
116+
}
117+
return result
118+
}
119+
120+
func analisisEstudiantes() {
121+
// Definimos una lista de estudiantes
122+
studentsList := []Student{
123+
{"Juan", "2000-01-01", []float64{8.5, 9.0, 7.5}},
124+
{"Maria", "2001-02-03", []float64{9.0, 9.5, 8.0}},
125+
{"Pedro", "1999-03-05", []float64{7.0, 8.0, 6.5}},
126+
{"Ana", "2000-04-07", []float64{9.5, 9.0, 8.5}},
127+
{"Luis", "1998-05-09", []float64{6.0, 7.0, 5.5}},
128+
}
129+
130+
// Realizamos el analisis de los estudiantes
131+
fmt.Println("Promedio de calificaciones: ", applyAnalysis(studentsList, average))
132+
fmt.Println("Mejores estudiantes: ", applyAnalysis(studentsList, bestStudents))
133+
fmt.Println("Estudiantes ordenados empezando con los mas jovenes: ", applyAnalysis(studentsList, birthDateSort))
134+
fmt.Println("Calificacion(es) mas alta(s): ", applyAnalysis(studentsList, highestGrade))
135+
}

0 commit comments

Comments
 (0)