public No31_DefineApiConbinableSequence() { //目的: //①遅延実行で組み合わせたクエリを1度のみ操作できる //概要: //-------------------------------------------------------------------------------------- //イテレータメソッド(yield return)を実装することで、 //再利用可能な遅延実行メソッドを実装できる //利用時 var nums = new List <int>() { 1, 1, 2, 3, 3, 4, 5, 6, 7, 8 }; var list = new List <string>() { "a", "b", "a", "d", "c", "d", "j", "i", "d", "j", }; //NG //処理が1つに内包されていて、再利用不可能 ProduceMethod.UniqueWrite(nums); //OK //イテレータメソッドとすることで再利用可能に IteratorMethod.UniqueWrite(nums); //さらにジェネリクスにすることで再利用可能に var res = IteratorMethod.Unique(list); }
private void DoApply(IteratorMethod method, int i, IActor actor) { CollectionBehaviors <T> linkedBehavior = LinkedTo as CollectionBehaviors <T>; switch (method) { case IteratorMethod.MoveNext: { if ((i < linkedBehavior.List.Count) && (i >= 0)) { actor.SendMessage(IteratorMethod.OkMoveNext, true); } else { actor.SendMessage(IteratorMethod.OkMoveNext, false); } break; } case IteratorMethod.Current: { if ((i >= 0) && (i < linkedBehavior.List.Count)) { actor.SendMessage(IteratorMethod.OkCurrent, linkedBehavior.List[i]); } else { Debug.WriteLine("Bad current"); } break; } default: throw new ActorException(string.Format(CultureInfo.InvariantCulture, "Bad IteratorMethod call {0}", method)); } }
public static void ForEach <TIn, TOut>( IteratorMethod <TIn, TOut> iteratorMethod, Action <TIn, Action <TOut> > forEach) { IEnumerator <TIn> enumerator = null; try { enumerator = new YieldEnumerable <TIn, TOut>(iteratorMethod).GetEnumerator(); var enumerator2 = (YieldEnumerator <TIn, TOut>)enumerator; while (enumerator.MoveNext()) { try { forEach(enumerator2.Current, enumerator2.Return); } catch (Exception x) // Oh the humanity! { enumerator2.Throw(x); } } } finally { enumerator?.Dispose(); } }
public TimeProfiler(IteratorMethod method) { if (method == IteratorMethod.foreachNonLazyCoding) { profile_foreachNonLazyCoding(); } if (method == IteratorMethod.foreachLazyCoding) { profile_foreachLazyCoding(); } if (method == IteratorMethod.forLoopCoding) { profile_forLoopCoding(); } }
/// <summary>Iterate through all Entities in this EntityContainer.</summary> /// <remarks>This method can modify objects during iteration! /// If this functionality is undesired, iterate then through this /// EntityContainer using a 'foreach'-loop (from IEnumerable).</remarks> public void Iterate(IteratorMethod iterator) { var count = entities.Count; var newList = new List <Entity>(count); // iterate through entities for (int i = 0; i < count; i++) { iterator(entities[i]); } // keep Entities that have not been marked for deletion during iteration foreach (var entity in entities) { if (!entity.IsDeleted()) { newList.Add(entity); } } entities = newList; }
public YieldEnumerator(IteratorMethod <TItem, TResult> iteratorMethod) { _task = iteratorMethod(this); }
public YieldEnumerable(IteratorMethod <TItem, TResult> iteratorMethod) { _iteratorMethod = iteratorMethod; }