-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6c20dc
commit bef773c
Showing
6 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
conferences/2024/16-ienumerable-and-ienumerator/code/Program01.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Collections; | ||
|
||
namespace Programacion | ||
{ | ||
internal class Program01 | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
#region IENUMERABLE E IENUMERATOR SIN GENERICIDAD | ||
//LOS ARRAYS SON IMPLEMENTAN LA INTERFACE IENUMERATOR | ||
string[] colores = new string[] { "rojo", "azul", "blanco", "negro" }; | ||
|
||
//RECORRIDO CON IENUMERATOR | ||
IEnumerator enumColores = colores.GetEnumerator(); | ||
Console.WriteLine("\nRecorrido con IEnumerator ..."); | ||
while (enumColores.MoveNext()) | ||
{ | ||
//Compila y ejecuta bien porque string implementa el ToString de object | ||
Console.WriteLine(enumColores.Current); | ||
|
||
//ERROR DE COMPILACION | ||
//Para el compiler lo que devuelve Current es object | ||
//y object NO TIENE Length | ||
//Console.WriteLine(enumColores.Current.Length); | ||
} | ||
|
||
////RECORRIDO DIRECTO DEL IENUMERABLE CON FOREACH | ||
//Console.WriteLine("\nRecorrido con IEnumerable y foreach ..."); | ||
//foreach (object x in colores) | ||
// Console.WriteLine(x); | ||
|
||
////EL CODIGO ANTERIOR ES EQUIVALENTE A. El COMPILADOR HACE LA TRANSFORMACION | ||
//IEnumerator items = colores.GetEnumerator(); | ||
//Console.WriteLine(); | ||
//while (items.MoveNext()) | ||
//{ | ||
// object x = items.Current; | ||
// Console.WriteLine(x); | ||
//} | ||
#endregion | ||
|
||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
conferences/2024/16-ienumerable-and-ienumerator/code/Program02.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Collections; | ||
namespace Programacion | ||
{ | ||
internal class Program02 | ||
{ | ||
public class ParesEnIntervalo : IEnumerable | ||
{ | ||
public int Min { get; } | ||
public int Max { get; } | ||
public ParesEnIntervalo(int min, int max) | ||
{ | ||
//Se podria verificar si forman un intervalo | ||
Min = min; Max = max; | ||
} | ||
|
||
#region IMPLEMENTACIÓN LOW LEVEL (SIN USAR YIELD) | ||
public IEnumerator GetEnumerator() | ||
{ | ||
return new ParesEnumerator(Min, Max); | ||
} | ||
|
||
class ParesEnumerator : IEnumerator | ||
{ | ||
public int Min { get; } | ||
public int Max { get; } | ||
int cursor; bool huboMoveNext; | ||
int current; | ||
public ParesEnumerator(int min, int max) | ||
{ | ||
Min = min; Max = max; | ||
if (Min % 2 == 0) cursor = min; | ||
else cursor = Min + 1; | ||
//Garantizando empezar con un par | ||
huboMoveNext = false; | ||
} | ||
public bool MoveNext() | ||
{ | ||
if (cursor <= Max) | ||
{ | ||
current = cursor; | ||
cursor += 2; | ||
return huboMoveNext = true; | ||
} | ||
else return huboMoveNext = false; | ||
} | ||
public object Current | ||
{ | ||
//Un ejemplo de por que Current no solo devuelve un valor | ||
get | ||
{ | ||
if (huboMoveNext) return current; | ||
else throw new Exception("There are no more elements"); | ||
} | ||
} | ||
public void Reset() | ||
{ | ||
//Ponerlo todo para empezar de nuevo | ||
if (Min % 2 == 0) cursor = Min; | ||
else cursor = Min + 1; | ||
huboMoveNext = false; | ||
} | ||
void Dispose() | ||
{ | ||
//De momento no se implementa ninguna acción en Dispose | ||
} | ||
} | ||
|
||
#endregion | ||
static void Main(string[] args) | ||
{ | ||
while (true) | ||
{ | ||
Console.Write("Entre cota inferior --> "); | ||
int inf = int.Parse(Console.ReadLine()); | ||
Console.Write("Entre cota superior --> "); | ||
int sup = int.Parse(Console.ReadLine()); | ||
var pares = new ParesEnIntervalo(inf, sup); | ||
Console.WriteLine("Los pares en ({0},{1}) son", inf, sup); | ||
//La maquinaria del recorrido esta encapsulada en el enumerable | ||
foreach (var n in pares) | ||
Console.WriteLine(n); | ||
//Los puedo escribir porque el n es object y los int vistos como | ||
//object implementan el ToString | ||
|
||
////Si si se descomenta lo siguiente da ERROR | ||
//foreach (var n in pares) | ||
// Console.WriteLine(n+1); | ||
} | ||
} | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
conferences/2024/16-ienumerable-and-ienumerator/code/Program03.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
namespace Programacion | ||
{ | ||
internal class Program03 | ||
{ | ||
public class ParesEnIntervalo : IEnumerable<int> | ||
{ | ||
public int Min { get; } | ||
public int Max { get; } | ||
public ParesEnIntervalo(int min, int max) | ||
{ | ||
//Se podria verificar si forman un intervalo | ||
Min = min; Max = max; | ||
} | ||
|
||
#region IMPLEMENTACIÓN LOW LEVEL (SIN USAR YIELD) | ||
public IEnumerator<int> GetEnumerator() | ||
{ | ||
return new ParesEnumerator(Min, Max); | ||
} | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
class ParesEnumerator : IEnumerator<int> | ||
{ | ||
public int Min { get; } | ||
public int Max { get; } | ||
int cursor; bool huboMoveNext; | ||
int current; | ||
public ParesEnumerator(int min, int max) | ||
{ | ||
Min = min; Max = max; | ||
if (Min % 2 == 0) cursor = min; | ||
else cursor = Min + 1; | ||
//Garantizando empezar con un par | ||
huboMoveNext = false; | ||
} | ||
public bool MoveNext() | ||
{ | ||
if (cursor <= Max) | ||
{ | ||
current = cursor; | ||
cursor += 2; | ||
return huboMoveNext = true; | ||
} | ||
else return huboMoveNext = false; | ||
} | ||
public int Current | ||
{ | ||
//Un ejemplo de por que Current no solo devuelve un valor | ||
get | ||
{ | ||
if (huboMoveNext) return current; | ||
else throw new Exception("There are no more elements"); | ||
} | ||
} | ||
object IEnumerator.Current | ||
{ | ||
//Un ejemplo de por que Current no solo devuelve un valor | ||
get { return Current; } | ||
} | ||
public void Reset() | ||
{ | ||
//Ponerlo todo para empezar de nuevo | ||
if (Min % 2 == 0) cursor = Min; | ||
else cursor = Min + 1; | ||
huboMoveNext = false; | ||
} | ||
public void Dispose() | ||
{ | ||
//De momento no se implementa ninguna acción en Dispose | ||
} | ||
} | ||
|
||
#endregion | ||
static void Main(string[] args) | ||
{ | ||
#region INTERVALO DE ENTEROS | ||
//while (true) | ||
//{ | ||
// Console.Write("Entre cota inferior --> "); | ||
// int inf = int.Parse(Console.ReadLine()); | ||
// Console.Write("Entre cota superior --> "); | ||
// int sup = int.Parse(Console.ReadLine()); | ||
// var pares = new ParesEnIntervalo(inf, sup); | ||
// Console.WriteLine("Los cuadrados de los pares en ({0},{1}) son", inf, sup); | ||
// foreach (var n in pares) | ||
// Console.WriteLine(n*n); | ||
//} | ||
#endregion | ||
|
||
#region string[] es IENUMERABLE<string> | ||
//IEnumerable<string> colores = new string[] { "rojo", "azul", "blanco", "negro" }; | ||
//Console.WriteLine("\nColores por foreach"); | ||
//foreach (var s in colores) | ||
//{ | ||
// Console.WriteLine("{0} tiene longitud {1}", s, s.Length); | ||
//} | ||
////Usando la maquinaria de IEnumerator | ||
//var colors = colores.GetEnumerator(); | ||
//Console.WriteLine("Colores por IEnumerator"); | ||
//while (colors.MoveNext()) | ||
//{ | ||
// Console.WriteLine("{0} tiene longitud {1}", | ||
// colors.Current, | ||
// colors.Current.Length); | ||
//} | ||
#endregion | ||
} | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
conferences/2024/16-ienumerable-and-ienumerator/code/Program04.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
namespace Programacion | ||
{ | ||
class FirstIEnumerable<T> : IEnumerable<T> | ||
{ | ||
class FirstEnumerator<T> : IEnumerator<T> | ||
{ | ||
int count, cursor; | ||
T current; | ||
bool huboMoveNext; | ||
IEnumerator<T> internalEnumerator; | ||
public FirstEnumerator(int n, IEnumerable<T> items) | ||
{ | ||
count = n; cursor = 0; huboMoveNext = false; | ||
internalEnumerator = items.GetEnumerator(); | ||
} | ||
public bool MoveNext() | ||
{ | ||
if ((cursor < count) && internalEnumerator.MoveNext()) | ||
{ | ||
current = internalEnumerator.Current; | ||
cursor++; | ||
return (huboMoveNext = true); | ||
} | ||
return false; | ||
} | ||
public T Current | ||
{ | ||
get | ||
{ | ||
if (huboMoveNext) return current; | ||
else throw new InvalidOperationException("No more elements"); | ||
} | ||
} | ||
object IEnumerator.Current | ||
{ | ||
get { return Current; } | ||
} | ||
public void Reset() | ||
{ | ||
} | ||
public void Dispose() | ||
{ | ||
} | ||
} | ||
IEnumerator<T> enumerator; | ||
public FirstIEnumerable(int n, IEnumerable<T> items) | ||
{ | ||
enumerator = new FirstEnumerator<T>(n, items); | ||
} | ||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return enumerator; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return enumerator; | ||
} | ||
} | ||
internal class Program04 | ||
{ | ||
static public IEnumerable<T> First<T>(int n, IEnumerable<T> elems) | ||
{ | ||
return new FirstIEnumerable<T>(n, elems); | ||
} | ||
|
||
static public IEnumerable<T> MagicFirst<T>(int n, IEnumerable<T> elems) | ||
{ | ||
foreach (T x in elems) | ||
{ | ||
n--; | ||
if (n>=0) yield return x; | ||
} | ||
} | ||
|
||
static void Main(string[] args) | ||
{ | ||
IEnumerable<int> nums = new int[] | ||
{ | ||
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 | ||
}; | ||
Console.WriteLine("Los primeros 11 via basica"); | ||
foreach (int k in First<int>(11, nums)) | ||
Console.WriteLine(k); | ||
|
||
Console.WriteLine("\nLos primeros 15 via magic"); | ||
foreach (int k in MagicFirst(15, nums)) | ||
Console.WriteLine(k); | ||
//Comentar el codigo de las clases enumeradoras y probar | ||
//que Magic no depende de eso | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
conferences/2024/16-ienumerable-and-ienumerator/code/Program05.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace Programacion | ||
{ | ||
internal class Program05 | ||
{ | ||
static public IEnumerable<int>Pares() | ||
{ | ||
int n = 0; | ||
while (true) | ||
{ | ||
n += 2; | ||
yield return n; | ||
} | ||
} | ||
static public IEnumerable<T> First<T>(int n, IEnumerable<T> elems) | ||
{ | ||
foreach (T x in elems) | ||
{ | ||
n--; | ||
if (n >= 0) yield return x; | ||
else break; //Ilustrar que pasa si se quita este break | ||
} | ||
} | ||
static void Main(string[] args) | ||
{ | ||
while (true) | ||
{ | ||
Console.Write("\nCuantos pares a listar --> "); | ||
int n = int.Parse(Console.ReadLine()); | ||
Console.WriteLine("Los {0} primeros pares son",n); | ||
foreach (int k in First(n, Pares())) | ||
{ | ||
Console.WriteLine(k); | ||
} | ||
} | ||
Console.WriteLine("Hello, World!"); | ||
} | ||
} | ||
} | ||
|
||
//EJERCICIOS | ||
//Para un enumerable de enteros devolver aquellos que sean primos | ||
//Para un enumerable de string devolver aquellos string que contengan una determinada subcadena | ||
//Para un enumerable de string devuelva un enumerable de int con las longitudes de los string | ||
//Para un enumerable de enumerables de int devuelva un enumerable con las sumas de los enumerables | ||
// |
Oops, something went wrong.