Skip to content

Commit bef773c

Browse files
committed
Add Lecture 16 (+Code)
1 parent e6c20dc commit bef773c

File tree

6 files changed

+579
-0
lines changed

6 files changed

+579
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections;
2+
3+
namespace Programacion
4+
{
5+
internal class Program01
6+
{
7+
static void Main(string[] args)
8+
{
9+
#region IENUMERABLE E IENUMERATOR SIN GENERICIDAD
10+
//LOS ARRAYS SON IMPLEMENTAN LA INTERFACE IENUMERATOR
11+
string[] colores = new string[] { "rojo", "azul", "blanco", "negro" };
12+
13+
//RECORRIDO CON IENUMERATOR
14+
IEnumerator enumColores = colores.GetEnumerator();
15+
Console.WriteLine("\nRecorrido con IEnumerator ...");
16+
while (enumColores.MoveNext())
17+
{
18+
//Compila y ejecuta bien porque string implementa el ToString de object
19+
Console.WriteLine(enumColores.Current);
20+
21+
//ERROR DE COMPILACION
22+
//Para el compiler lo que devuelve Current es object
23+
//y object NO TIENE Length
24+
//Console.WriteLine(enumColores.Current.Length);
25+
}
26+
27+
////RECORRIDO DIRECTO DEL IENUMERABLE CON FOREACH
28+
//Console.WriteLine("\nRecorrido con IEnumerable y foreach ...");
29+
//foreach (object x in colores)
30+
// Console.WriteLine(x);
31+
32+
////EL CODIGO ANTERIOR ES EQUIVALENTE A. El COMPILADOR HACE LA TRANSFORMACION
33+
//IEnumerator items = colores.GetEnumerator();
34+
//Console.WriteLine();
35+
//while (items.MoveNext())
36+
//{
37+
// object x = items.Current;
38+
// Console.WriteLine(x);
39+
//}
40+
#endregion
41+
42+
}
43+
}
44+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Collections;
2+
namespace Programacion
3+
{
4+
internal class Program02
5+
{
6+
public class ParesEnIntervalo : IEnumerable
7+
{
8+
public int Min { get; }
9+
public int Max { get; }
10+
public ParesEnIntervalo(int min, int max)
11+
{
12+
//Se podria verificar si forman un intervalo
13+
Min = min; Max = max;
14+
}
15+
16+
#region IMPLEMENTACIÓN LOW LEVEL (SIN USAR YIELD)
17+
public IEnumerator GetEnumerator()
18+
{
19+
return new ParesEnumerator(Min, Max);
20+
}
21+
22+
class ParesEnumerator : IEnumerator
23+
{
24+
public int Min { get; }
25+
public int Max { get; }
26+
int cursor; bool huboMoveNext;
27+
int current;
28+
public ParesEnumerator(int min, int max)
29+
{
30+
Min = min; Max = max;
31+
if (Min % 2 == 0) cursor = min;
32+
else cursor = Min + 1;
33+
//Garantizando empezar con un par
34+
huboMoveNext = false;
35+
}
36+
public bool MoveNext()
37+
{
38+
if (cursor <= Max)
39+
{
40+
current = cursor;
41+
cursor += 2;
42+
return huboMoveNext = true;
43+
}
44+
else return huboMoveNext = false;
45+
}
46+
public object Current
47+
{
48+
//Un ejemplo de por que Current no solo devuelve un valor
49+
get
50+
{
51+
if (huboMoveNext) return current;
52+
else throw new Exception("There are no more elements");
53+
}
54+
}
55+
public void Reset()
56+
{
57+
//Ponerlo todo para empezar de nuevo
58+
if (Min % 2 == 0) cursor = Min;
59+
else cursor = Min + 1;
60+
huboMoveNext = false;
61+
}
62+
void Dispose()
63+
{
64+
//De momento no se implementa ninguna acción en Dispose
65+
}
66+
}
67+
68+
#endregion
69+
static void Main(string[] args)
70+
{
71+
while (true)
72+
{
73+
Console.Write("Entre cota inferior --> ");
74+
int inf = int.Parse(Console.ReadLine());
75+
Console.Write("Entre cota superior --> ");
76+
int sup = int.Parse(Console.ReadLine());
77+
var pares = new ParesEnIntervalo(inf, sup);
78+
Console.WriteLine("Los pares en ({0},{1}) son", inf, sup);
79+
//La maquinaria del recorrido esta encapsulada en el enumerable
80+
foreach (var n in pares)
81+
Console.WriteLine(n);
82+
//Los puedo escribir porque el n es object y los int vistos como
83+
//object implementan el ToString
84+
85+
////Si si se descomenta lo siguiente da ERROR
86+
//foreach (var n in pares)
87+
// Console.WriteLine(n+1);
88+
}
89+
}
90+
}
91+
}
92+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
namespace Programacion
4+
{
5+
internal class Program03
6+
{
7+
public class ParesEnIntervalo : IEnumerable<int>
8+
{
9+
public int Min { get; }
10+
public int Max { get; }
11+
public ParesEnIntervalo(int min, int max)
12+
{
13+
//Se podria verificar si forman un intervalo
14+
Min = min; Max = max;
15+
}
16+
17+
#region IMPLEMENTACIÓN LOW LEVEL (SIN USAR YIELD)
18+
public IEnumerator<int> GetEnumerator()
19+
{
20+
return new ParesEnumerator(Min, Max);
21+
}
22+
IEnumerator IEnumerable.GetEnumerator()
23+
{
24+
return GetEnumerator();
25+
}
26+
class ParesEnumerator : IEnumerator<int>
27+
{
28+
public int Min { get; }
29+
public int Max { get; }
30+
int cursor; bool huboMoveNext;
31+
int current;
32+
public ParesEnumerator(int min, int max)
33+
{
34+
Min = min; Max = max;
35+
if (Min % 2 == 0) cursor = min;
36+
else cursor = Min + 1;
37+
//Garantizando empezar con un par
38+
huboMoveNext = false;
39+
}
40+
public bool MoveNext()
41+
{
42+
if (cursor <= Max)
43+
{
44+
current = cursor;
45+
cursor += 2;
46+
return huboMoveNext = true;
47+
}
48+
else return huboMoveNext = false;
49+
}
50+
public int Current
51+
{
52+
//Un ejemplo de por que Current no solo devuelve un valor
53+
get
54+
{
55+
if (huboMoveNext) return current;
56+
else throw new Exception("There are no more elements");
57+
}
58+
}
59+
object IEnumerator.Current
60+
{
61+
//Un ejemplo de por que Current no solo devuelve un valor
62+
get { return Current; }
63+
}
64+
public void Reset()
65+
{
66+
//Ponerlo todo para empezar de nuevo
67+
if (Min % 2 == 0) cursor = Min;
68+
else cursor = Min + 1;
69+
huboMoveNext = false;
70+
}
71+
public void Dispose()
72+
{
73+
//De momento no se implementa ninguna acción en Dispose
74+
}
75+
}
76+
77+
#endregion
78+
static void Main(string[] args)
79+
{
80+
#region INTERVALO DE ENTEROS
81+
//while (true)
82+
//{
83+
// Console.Write("Entre cota inferior --> ");
84+
// int inf = int.Parse(Console.ReadLine());
85+
// Console.Write("Entre cota superior --> ");
86+
// int sup = int.Parse(Console.ReadLine());
87+
// var pares = new ParesEnIntervalo(inf, sup);
88+
// Console.WriteLine("Los cuadrados de los pares en ({0},{1}) son", inf, sup);
89+
// foreach (var n in pares)
90+
// Console.WriteLine(n*n);
91+
//}
92+
#endregion
93+
94+
#region string[] es IENUMERABLE<string>
95+
//IEnumerable<string> colores = new string[] { "rojo", "azul", "blanco", "negro" };
96+
//Console.WriteLine("\nColores por foreach");
97+
//foreach (var s in colores)
98+
//{
99+
// Console.WriteLine("{0} tiene longitud {1}", s, s.Length);
100+
//}
101+
////Usando la maquinaria de IEnumerator
102+
//var colors = colores.GetEnumerator();
103+
//Console.WriteLine("Colores por IEnumerator");
104+
//while (colors.MoveNext())
105+
//{
106+
// Console.WriteLine("{0} tiene longitud {1}",
107+
// colors.Current,
108+
// colors.Current.Length);
109+
//}
110+
#endregion
111+
}
112+
}
113+
}
114+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
namespace Programacion
4+
{
5+
class FirstIEnumerable<T> : IEnumerable<T>
6+
{
7+
class FirstEnumerator<T> : IEnumerator<T>
8+
{
9+
int count, cursor;
10+
T current;
11+
bool huboMoveNext;
12+
IEnumerator<T> internalEnumerator;
13+
public FirstEnumerator(int n, IEnumerable<T> items)
14+
{
15+
count = n; cursor = 0; huboMoveNext = false;
16+
internalEnumerator = items.GetEnumerator();
17+
}
18+
public bool MoveNext()
19+
{
20+
if ((cursor < count) && internalEnumerator.MoveNext())
21+
{
22+
current = internalEnumerator.Current;
23+
cursor++;
24+
return (huboMoveNext = true);
25+
}
26+
return false;
27+
}
28+
public T Current
29+
{
30+
get
31+
{
32+
if (huboMoveNext) return current;
33+
else throw new InvalidOperationException("No more elements");
34+
}
35+
}
36+
object IEnumerator.Current
37+
{
38+
get { return Current; }
39+
}
40+
public void Reset()
41+
{
42+
}
43+
public void Dispose()
44+
{
45+
}
46+
}
47+
IEnumerator<T> enumerator;
48+
public FirstIEnumerable(int n, IEnumerable<T> items)
49+
{
50+
enumerator = new FirstEnumerator<T>(n, items);
51+
}
52+
public IEnumerator<T> GetEnumerator()
53+
{
54+
return enumerator;
55+
}
56+
57+
IEnumerator IEnumerable.GetEnumerator()
58+
{
59+
return enumerator;
60+
}
61+
}
62+
internal class Program04
63+
{
64+
static public IEnumerable<T> First<T>(int n, IEnumerable<T> elems)
65+
{
66+
return new FirstIEnumerable<T>(n, elems);
67+
}
68+
69+
static public IEnumerable<T> MagicFirst<T>(int n, IEnumerable<T> elems)
70+
{
71+
foreach (T x in elems)
72+
{
73+
n--;
74+
if (n>=0) yield return x;
75+
}
76+
}
77+
78+
static void Main(string[] args)
79+
{
80+
IEnumerable<int> nums = new int[]
81+
{
82+
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
83+
};
84+
Console.WriteLine("Los primeros 11 via basica");
85+
foreach (int k in First<int>(11, nums))
86+
Console.WriteLine(k);
87+
88+
Console.WriteLine("\nLos primeros 15 via magic");
89+
foreach (int k in MagicFirst(15, nums))
90+
Console.WriteLine(k);
91+
//Comentar el codigo de las clases enumeradoras y probar
92+
//que Magic no depende de eso
93+
}
94+
}
95+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Programacion
2+
{
3+
internal class Program05
4+
{
5+
static public IEnumerable<int>Pares()
6+
{
7+
int n = 0;
8+
while (true)
9+
{
10+
n += 2;
11+
yield return n;
12+
}
13+
}
14+
static public IEnumerable<T> First<T>(int n, IEnumerable<T> elems)
15+
{
16+
foreach (T x in elems)
17+
{
18+
n--;
19+
if (n >= 0) yield return x;
20+
else break; //Ilustrar que pasa si se quita este break
21+
}
22+
}
23+
static void Main(string[] args)
24+
{
25+
while (true)
26+
{
27+
Console.Write("\nCuantos pares a listar --> ");
28+
int n = int.Parse(Console.ReadLine());
29+
Console.WriteLine("Los {0} primeros pares son",n);
30+
foreach (int k in First(n, Pares()))
31+
{
32+
Console.WriteLine(k);
33+
}
34+
}
35+
Console.WriteLine("Hello, World!");
36+
}
37+
}
38+
}
39+
40+
//EJERCICIOS
41+
//Para un enumerable de enteros devolver aquellos que sean primos
42+
//Para un enumerable de string devolver aquellos string que contengan una determinada subcadena
43+
//Para un enumerable de string devuelva un enumerable de int con las longitudes de los string
44+
//Para un enumerable de enumerables de int devuelva un enumerable con las sumas de los enumerables
45+
//

0 commit comments

Comments
 (0)