Skip to content

Commit ccbabdf

Browse files
authored
Merge pull request mouredev#6838 from Hequebo/hequebo22
#22 - C#
2 parents 58c7402 + 86399b3 commit ccbabdf

File tree

1 file changed

+85
-0
lines changed
  • Roadmap/22 - FUNCIONES DE ORDEN SUPERIOR/c#

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class Program
2+
{
3+
static void Main(string[] args)
4+
{
5+
/*
6+
* Una función de orden superior o HOF por sus siglas
7+
* en inglés es una función la cual recibe en sus
8+
* argumentos una o más funciones o regresa una función
9+
* o ambos
10+
*/
11+
Console.WriteLine($"{Operation(3, 6, Addition)}");
12+
Console.WriteLine($"{Operation(3, 6, Substraction)}");
13+
Console.WriteLine($"{Operation(3, 6, Multiplication)}");
14+
15+
// Ejercicio Extra
16+
Console.ReadLine();
17+
Console.Clear();
18+
var students = new List<Student>
19+
{
20+
new Student
21+
{
22+
Name ="Emilio Quezada",
23+
Birthdate = new DateTime(1997, 07, 28),
24+
Grades= new List<decimal>{8.5m, 9.8m, 8.3m, 8.5m }
25+
},
26+
new Student
27+
{
28+
Name ="Aldo Díaz",
29+
Birthdate = new DateTime(1998, 10, 5),
30+
Grades= new List<decimal>{9.5m, 9.9m, 8.3m, 8.9m }
31+
},
32+
new Student
33+
{
34+
Name ="Samantha Ortega",
35+
Birthdate = new DateTime(1997, 03, 30),
36+
Grades= new List<decimal>{8.6m, 9, 9.5m, 8.5m }
37+
},
38+
};
39+
/*
40+
* En .Net LINQ utiiliza funciones de orden superarior en las cuales reciben dentro
41+
* de sus parámetros una expresión lambda
42+
*/
43+
var averageList = students.Select(s => new { Name = s.Name, Average = Average(s.Grades) });
44+
Console.WriteLine("---Promedios estudiantes---");
45+
foreach (var student in averageList )
46+
Console.WriteLine($"Nombre: {student.Name}, Promerdio: {student.Average}");
47+
48+
Console.WriteLine("---Cuadro de honor---");
49+
var bestStudents = students.Where(s => Average(s.Grades) >= 9).Select(s => new { Name = s.Name, Average = Average(s.Grades)});
50+
foreach (var student in bestStudents)
51+
Console.WriteLine($"Nombre: {student.Name}, Promedio: {student.Average}");
52+
53+
Console.WriteLine("---Estudiante ordenados por edad---");
54+
var orderedStudents = students.OrderBy(s => s.Birthdate);
55+
foreach (var student in orderedStudents)
56+
Console.WriteLine($"Nombre: {student.Name}, Fecha de Nacimiento: {student.Birthdate.ToShortDateString()}");
57+
58+
Console.WriteLine("---Estudiate con calificación más alta---");
59+
var highestGrade = students.OrderByDescending(s => Max(s.Grades)).Select(s => new { Name = s.Name, Grade = Max(s.Grades) }).First();
60+
Console.WriteLine($"Nombre: {highestGrade.Name}, Calificación: {highestGrade.Grade}");
61+
62+
}
63+
static int Addition(int x, int y) => x + y;
64+
static int Substraction(int x, int y) => x - y;
65+
static int Multiplication(int x, int y) => x * y;
66+
/*
67+
* La función Operation recibe en sus argumentos
68+
* dos datos de tipo entero y una funcion(Func)
69+
* la cual recibe dos datos de tipo entero y
70+
* devuelve un tipo entero.
71+
* Así mismo la función Operation devuelve una
72+
* función.
73+
*/
74+
static int Operation(int x, int y, Func<int, int, int> fn) => fn(x, y);
75+
static decimal Average(List<decimal> grades) => grades.Sum() / grades.Count;
76+
static decimal Max(List<decimal> grades) => grades.Max(g => g);
77+
78+
79+
}
80+
class Student
81+
{
82+
public string Name { get; set; }
83+
public DateTime Birthdate { get; set; }
84+
public List<decimal> Grades { get; set; }
85+
}

0 commit comments

Comments
 (0)