Example #1
0
        public ConversationAggregationResult TrackAggregation(string operationName, AggregationDelegate aggregation)
        {
            ConversationAggregationResult result;

            using (this.CreateDiagnosticsFrame("ConversationAggregation", operationName))
            {
                try
                {
                    result = aggregation(this.Logger);
                }
                catch (Exception value)
                {
                    this.Logger.LogEvent(new SchemaBasedLogEvent <ConversationAggregationLogSchema.Error>
                    {
                        {
                            ConversationAggregationLogSchema.Error.Context,
                            base.GetType().Name
                        },
                        {
                            ConversationAggregationLogSchema.Error.Exception,
                            value
                        }
                    });
                    throw;
                }
            }
            return(result);
        }
Example #2
0
        public int AggregateCollection(List <int> original, AggregationDelegate aggregate)
        {
            int result = 0;

            foreach (var item in original)
            {
                result = SumItems(result, item);
            }

            return(result);
        }
        static void Main(string[] args)
        {
            List <int> numbers = new List <int>()
            {
                55, 11, 5, 15, 21, 3, 5, 6, 7, 8, 13, 14
            };

            AggregationDelegate myDelegate = CalculateAvarage;

            Console.WriteLine("Avarage : {0:F2}", AggregateCollection(numbers, myDelegate));
        }
Example #4
0
        public int AggregateCollection(List<int> original, AggregationDelegate aggregate)
        {
            int result = 0;

            foreach (var item in original)
            {
                result = SumItems(result, item);
            }

            return result;
        }
Example #5
0
 static int AggregateCollection(List<int> original, AggregationDelegate<int> aggregate)
 {
     if (original.Count == 0) return 0;
     if (original.Count == 1) return original[0];
     int curAggregation = aggregate(original[0], original[1]);
     if (original.Count == 1) return curAggregation;
     for (int i = 2; i < original.Count; i++)
     {
         curAggregation = aggregate(curAggregation, original[i]);
     }
     return curAggregation;
 }
Example #6
0
        public static T AggregateCollection <T>(this List <T> original, AggregationDelegate <T> aggregate)
        {
            if (original.Count <= 0)
            {
                throw new ArgumentException("List can't be empty");
            }

            var result = original[0];

            for (var i = 1; i < original.Count; i++)
            {
                result = aggregate(result, original[i], i + 1);
            }

            return(result);
        }
Example #7
0
        //public delegate void AnotherAggregationDelegate(List<int> numbers);

        static void Main(string[] args)
        {
            var numbers = new List <int> {
                2, 3, 14, 12, 19, 6, 4
            };

            AggregationDelegate myDelegate = CalculateAverage;

            // Test void delegates

            //AnotherAggregationDelegate mySecondDelegate = Min;
            //mySecondDelegate += Max;
            //mySecondDelegate += Sum;
            //mySecondDelegate += Average;

            Console.WriteLine("Average of numbers is {0}", AggregateCollection(numbers, myDelegate));
        }
        private ExpressionSyntax RewriteAsLoop(TypeSyntax returnType, IEnumerable <StatementSyntax> prologue, IEnumerable <StatementSyntax> epilogue, ExpressionSyntax collection, List <LinqStep> chain, AggregationDelegate k, bool noaggregation = false, IEnumerable <Tuple <ParameterSyntax, ExpressionSyntax> > additionalParameters = null)
        {
            var old = currentAggregation;

            currentAggregation = k;

            var collectionType     = semantic.GetTypeInfo(collection).Type;
            var collectionItemType = GetItemType(collectionType);

            if (collectionItemType == null)
            {
                throw new NotSupportedException();
            }
            var collectionSemanticType = semantic.GetTypeInfo(collection).Type;

            var parameters = new[] { CreateParameter(ItemsName, collectionSemanticType) }.Concat(currentFlow.Select(x => CreateParameter(x.Name, GetSymbolType(x.Symbol)).WithRef(x.Changes)));

            if (additionalParameters != null)
            {
                parameters = parameters.Concat(additionalParameters.Select(x => x.Item1));
            }



            var functionName = GetUniqueName(currentMethodName + "_ProceduralLinq");
            var arguments    = CreateArguments(new[] { SyntaxFactory.Argument(SyntaxFactory.IdentifierName(ItemName)) }.Concat(currentFlow.Select(x => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(x.Name)).WithRef(x.Changes))));

            var loopContent = CreateProcessingStep(chain, chain.Count - 1, SyntaxFactory.ParseTypeName(collectionItemType.ToDisplayString()), ItemName, arguments, noaggregation);

            StatementSyntax foreachStatement;

            if (collectionType.ToDisplayString().StartsWith("System.Collections.Generic.List<") || collectionType is IArrayTypeSymbol)
            {
                foreachStatement = SyntaxFactory.ForStatement(
                    SyntaxFactory.VariableDeclaration(CreatePrimitiveType(SyntaxKind.IntKeyword), CreateSeparatedList <VariableDeclaratorSyntax>(SyntaxFactory.VariableDeclarator("_index").WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(0)))))), default(SeparatedSyntaxList <ExpressionSyntax>),
                    SyntaxFactory.BinaryExpression(SyntaxKind.LessThanExpression, SyntaxFactory.IdentifierName("_index"), GetCollectionCount(collection, false)), CreateSeparatedList <ExpressionSyntax>(SyntaxFactory.PostfixUnaryExpression(SyntaxKind.PostIncrementExpression, SyntaxFactory.IdentifierName("_index"))),
                    SyntaxFactory.Block(new StatementSyntax[] { CreateLocalVariableDeclaration(ItemName, SyntaxFactory.ElementAccessExpression(SyntaxFactory.IdentifierName(ItemsName), SyntaxFactory.BracketedArgumentList(CreateSeparatedList(SyntaxFactory.Argument(SyntaxFactory.IdentifierName("_index")))))) }.Union((loopContent as BlockSyntax)?.Statements ?? (IEnumerable <StatementSyntax>) new[] { loopContent })));
            }
            else
            {
                foreachStatement = SyntaxFactory.ForEachStatement(
                    SyntaxFactory.IdentifierName("var"),
                    ItemName,
                    SyntaxFactory.IdentifierName(ItemsName),
                    loopContent is BlockSyntax ? loopContent : SyntaxFactory.Block(loopContent));
            }



            var coreFunction = SyntaxFactory.MethodDeclaration(returnType, functionName)
                               .WithParameterList(CreateParameters(parameters))
                               .WithBody(SyntaxFactory.Block((collectionSemanticType.IsValueType ? Enumerable.Empty <StatementSyntax>() : new[] {
                SyntaxFactory.IfStatement(SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, SyntaxFactory.IdentifierName(ItemsName), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), CreateThrowException("System.ArgumentNullException"))
            }).Concat(prologue).Concat(new[] {
                foreachStatement
            }).Concat(epilogue)))
                               .WithStatic(currentMethodIsStatic)
                               .WithTypeParameterList(currentMethodTypeParameters)
                               .WithConstraintClauses(currentMethodConstraintClauses)
                               .NormalizeWhitespace();

            methodsToAddToCurrentType.Add(Tuple.Create(currentType, coreFunction));

            IEnumerable <ArgumentSyntax> args = new[] { SyntaxFactory.Argument((ExpressionSyntax)Visit(collection)) }.Concat(arguments.Arguments.Skip(1));

            if (additionalParameters != null)
            {
                args = args.Concat(additionalParameters.Select(x => SyntaxFactory.Argument(x.Item2)));
            }
            var inv = SyntaxFactory.InvocationExpression(GetMethodNameSyntaxWithCurrentTypeParameters(functionName), CreateArguments(args));

            currentAggregation = old;
            return(inv);
        }
Example #9
0
 public static decimal AggregateCollection(List<int> original, AggregationDelegate myDelegate)
 {
     return myDelegate(original);
 }
Example #10
0
 public static decimal AggregateCollection(List <int> original, AggregationDelegate aggregation)
 {
     return(aggregation(original));
 }
 public int AggregateCollection ( List<int> original , AggregationDelegate aggregate )
 {
     return aggregate(original);
 }
Example #12
0
        static decimal AggregateCollection(List <int> original, AggregationDelegate aggregate)
        {
            decimal answer = aggregate(original);

            return(answer);
        }