public void ForEach(ForEachAction <T> act) { for (int i = 0; i < index.Count; i++) { act(ref GetValue(i)); } }
public void ForEach(ForEachAction <int, int, double> action) { for (int y = 0; y < _Rows; y++) { for (int x = 0; x < _Cols; x++) { action(x, y, this[x, y]); } } }
public void ForEach(ForEachAction action, object hint) { lock (this.queue) { foreach (AsyncProcess item_0 in this.queue) { action(item_0, hint); } } }
public static void ForEach <T>(this IEnumerable <T> source, ForEachAction <T> action) { using var iter = source.GetEnumerator(); if (iter.MoveNext()) { var prev = iter.Current; while (iter.MoveNext()) { action(prev, false); prev = iter.Current; } action(prev, true); } }
/// <summary> /// 循环字符串中的每一行 /// </summary> /// <param name="text">字符串</param> /// <param name="action">循环时所要执行的动作,循环过程中执行动作后返回0时,表示退出循环</param> public static void ForEachLines(string text, ForEachAction <string> action) { string[] lines = GetLines(text); for (int i = 0; i < lines.Length; i++) { int result = action(i, lines[i]); //当动作返回0时,则退出循环 if (result == 0) { return; } } }
protected static void ForEach <ActorType> (ForEachAction <ActorType> action, IList <ForActorType> from) where ActorType : ForActorType { lock (ForEachLock) { foreach (ForActorType unit in from) { if (unit is ActorType) { action((ActorType)unit); } } } }
protected static void ForEach <TActor, TFromActor>(ForEachAction <TActor> action, IList <TFromActor> from) where TActor : TForActorHelper where TFromActor : TForActorHelper { lock (ForEachLock) { foreach (var unit in from) { if (unit is TActor) { // What the hell, http://stackoverflow.com/questions/4092393/value-of-type-t-cannot-be-converted-to action((TActor)(object)unit); } } } }
public static async Task <JToken> ExecuteAsync(this ForEachAction action, StateMachineContext context, JToken input) { action.CheckArgNull(nameof(action)); context.CheckArgNull(nameof(context)); input.CheckArgNull(nameof(input)); Debug.Assert(!string.IsNullOrWhiteSpace(action.Input)); var token = action.Input.EvalExpr(input, context); if (token == null || token.Type != JTokenType.Array) { throw new InvalidOperationException("Unable to resolve input collection for ForEach action."); } var inputCollection = (JArray)token; var max = action.MaxParallel ?? inputCollection.Count; Debug.Assert(action.Action != null); var outputs = new JArray(); for (var i = 0; i < inputCollection.Count; i += max) { if (context.CancelToken.IsCancellationRequested) { break; } var subsetTasks = inputCollection.Skip(i) .Take(max) .Select(json => action.Action.ExecuteAsync(context, json)); var results = await Task.WhenAll(subsetTasks); Array.ForEach(results, outputs.Add); } return(outputs); }
public static IEnumerable <TSource> ForEachNotNull <TSource>(this IEnumerable <TSource> source, ForEachAction <TSource> action) { Utils.RequireNotNull(source, "source"); Utils.RequireNotNull(action, "action"); foreach (TSource item in source) { if (item != null) { action(item); } } // return the source so we can chain to other LINQ operators return(source); }
public static IEnumerable <TItem> ForEachWithDetailsDo <TItem>(this IEnumerable <TItem> elements, ForEachAction <TItem> action) { if (elements == null) { throw new ArgumentNullException(nameof(elements)); } using (IEnumerator <TItem> enumerator = elements.GetEnumerator()) { bool isFirst = true; bool hasNext = enumerator.MoveNext(); int index = -1; while (hasNext) { TItem current = enumerator.Current; hasNext = enumerator.MoveNext(); var itemInfo = new ItemInfo(++index, isFirst, !hasNext); action?.Invoke(current, itemInfo); isFirst = false; } } return(elements); }
public static async Task <JToken> ExecuteAsync(this ModelAction action, StateMachineContext context, JToken input) { action.CheckArgNull(nameof(action)); context.CheckArgNull(nameof(context)); input.CheckArgNull(nameof(input)); await context.RecordObservableActionAsync(ObservableAction.BeforeAction, () => new Dictionary <string, object> { { "actionName", action.Name }, { "actionType", action.GetType().FullName } }); Func <StateMachineContext, JToken, Task <JToken> > executeFunc = action switch { InjectDataAction inject => inject.ExecuteAsync, ParallelAction parallel => parallel.ExecuteAsync, SequenceAction sequence => sequence.ExecuteAsync, SendEventAction send => send.ExecuteAsync, DelayAction delay => delay.ExecuteAsync, InvokeSubflowAction subflow => subflow.ExecuteAsync, InvokeFunctionAction function => function.ExecuteAsync, ForEachAction forEach => forEach.ExecuteAsync, _ => throw new NotImplementedException("Action behavior not implemented: " + action.GetType().FullName) }; Debug.Assert(executeFunc != null); var attempts = 0; DateTimeOffset?start = null; while (true) { try { var result = await executeFunc(context, input); await context.RecordObservableActionAsync(ObservableAction.AfterAction, () => new Dictionary <string, object> { { "actionName", action.Name }, { "actionType", action.GetType().FullName } }); return(result); } catch (Exception ex) { if (action.TryHandleError(JObject.FromObject(ex), context, out RetryPolicy? retryPolicy)) { if (retryPolicy == null) { return(JValue.CreateNull()); } else { TimeSpan elapsedDelay; if (start == null) { start = DateTimeOffset.UtcNow; elapsedDelay = TimeSpan.Zero; } else { elapsedDelay = DateTimeOffset.UtcNow.Subtract(start.Value); } var retry = await retryPolicy.ShouldRetryAsync(context, ++attempts, elapsedDelay); if (retry) { continue; } } } throw; } } }