اشیاء Enumerable و Enumerator و استفاده از قابلیتهای yield (قسمت دوم)
نویسنده: بیاگوی
تاریخ: ۱۳۹۱/۰۵/۱۸ ۱۱:۴۴
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class ArrayEnumerable<T> : IEnumerable<T>
{
T[] _array;
public ArrayEnumerable(T[] array)
{
_array = array;
}
public IEnumerator<T> GetEnumerator()
{
int index = 0;
while (index < _array.Length)
{
yield return _array[index];
index++;
}
yield break;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class ArrayEnumerable<T> : IEnumerable<T>, IEnumerable
{
// Fields
private T[] _array;
// Methods
public ArrayEnumerable(T[] array)
{
this._array = array;
}
public IEnumerator<T> GetEnumerator()
{
return new <GetEnumerator>d__0(0);
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
// Nested Types
[CompilerGenerated]
private sealed class <GetEnumerator>d__0 : IEnumerator<T>, IEnumerator, IDisposable
{
// Fields
private int <>1__state;
private T <>2__current;
public ArrayEnumerable<T> <>4__this;
public int <index>5__1;
// Methods
[DebuggerHidden]
public <GetEnumerator>d__0(int <>1__state)
{
this.<>1__state = <>1__state;
}
private bool MoveNext()
{
switch (this.<>1__state)
{
case 0:
this.<>1__state = -1;
this.<index>5__1 = 0;
while (this.<index>5__1 < ArrayEnumerable<T>._array.Length)
{
this.<>2__current = ArrayEnumerable<T>._array[this.<index>5__1];
this.<>1__state = 1;
return true;
Label_0050:
this.<>1__state = -1;
this.<index>5__1++;
}
break;
case 1:
goto Label_0050;
}
return false;
}
[DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
void IDisposable.Dispose()
{
}
// Properties
T IEnumerator<T>.Current
{
[DebuggerHidden]
get
{
return this.<>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return this.<>2__current;
}
}
}
}
def array_iterator(array):
length = len(array)
index = 0
while index < length:
yield array[index]
index = index + 1
>>> import dis
>>> dis.dis(array_iterator)
2 0 LOAD_GLOBAL 0 (len)
3 LOAD_FAST 0 (array)
6 CALL_FUNCTION 1
9 STORE_FAST 1 (length)
3 12 LOAD_CONST 1 (0)
15 STORE_FAST 2 (index)
4 18 SETUP_LOOP 35 (to 56)
>> 21 LOAD_FAST 2 (index)
24 LOAD_FAST 1 (length)
27 COMPARE_OP 0 (<)
30 POP_JUMP_IF_FALSE 55
5 33 LOAD_FAST 0 (array)
36 LOAD_FAST 2 (index)
39 BINARY_SUBSCR
40 YIELD_VALUE
41 POP_TOP
6 42 LOAD_FAST 2 (index)
45 LOAD_CONST 2 (1)
48 BINARY_ADD
49 STORE_FAST 2 (index)
52 JUMP_ABSOLUTE 21
>> 55 POP_BLOCK
>> 56 LOAD_CONST 0 (None)
59 RETURN_VALUE
public class SimpleStateMachine : IEnumerable<bool>
{
public IEnumerator<bool> GetEnumerator()
{
while (true)
{
yield return true;
yield return false;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
new SimpleStateMachine().Take(20).ToList().ForEach(x => Console.WriteLine(x));
public class ReallySimple<T> : IEnumerable<T>
{
//blah blah blah...
}
Enumerable.Range(1, 20).Select(r => new ReallySimple()).ToList().ForEach(x => Console.WriteLine(x));