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