public static IEnumerable <SearchItem> Last(SearchExpressionContext c)
        {
            var argIndex   = 0;
            var takeNumber = 1;

            if (c.args[0].types.HasFlag(SearchExpressionType.Number))
            {
                ++argIndex;
                takeNumber = Math.Max((int)(c.args[0].GetNumberValue(takeNumber)), 0);
            }

            var queue = new Queue <SearchItem>(takeNumber);

            return(c.ForEachArgument((context, e) =>
            {
                queue.Clear();
                if (takeNumber == 0)
                {
                    context.Break();
                }
            }, argIndex).AggregateResults(queue, (context, item, aggregator) =>
            {
                if (queue.Count == takeNumber)
                {
                    queue.Dequeue();
                }
                queue.Enqueue(item);
            }));
        }
        public static IEnumerable <SearchItem> First(SearchExpressionContext c)
        {
            var argIndex   = 0;
            var takeNumber = 1;

            if (c.args[0].types.HasFlag(SearchExpressionType.Number))
            {
                ++argIndex;
                takeNumber = Math.Max((int)(c.args[0].GetNumberValue(takeNumber)), 0);
            }

            var taken = 0;

            return(c.ForEachArgument((context, e) =>
            {
                taken = 0;
                if (takeNumber == 0)
                {
                    context.Break();
                }
            }, argIndex).ForEachResult((context, item) =>
            {
                if (taken >= takeNumber)
                {
                    context.Break();
                    return null;
                }

                ++taken;
                return item;
            }));
        }
Exemple #3
0
        public static IEnumerable <SearchItem> Avg(SearchExpressionContext c)
        {
            var    skipCount = 0;
            string selector  = null;

            if (c.args[0].types.HasFlag(SearchExpressionType.Selector))
            {
                skipCount++;
                selector = c.args[0].innerText.ToString();
            }
            return(c.ForEachArgument(skipCount).AggregateResults(Average.Zero,
                                                                 (context, item, avg) => Aggregate(item, selector, avg, (d, _avg) => _avg.Add(d)),
                                                                 (context, avg) => EvaluatorUtils.CreateItem(avg.result, context.ResolveAlias("Average"))));
        }
Exemple #4
0
        public static IEnumerable <SearchItem> Max(SearchExpressionContext c)
        {
            var    skipCount = 0;
            string selector  = null;

            if (c.args[0].types.HasFlag(SearchExpressionType.Selector))
            {
                skipCount++;
                selector = c.args[0].innerText.ToString();
            }
            return(c.ForEachArgument(skipCount).AggregateResults(double.MinValue,
                                                                 (context, item, max) => Aggregate(item, selector, max, (d, _max) => d > _max),
                                                                 (context, max) => EvaluatorUtils.CreateItem(max, context.ResolveAlias("Max"))));
        }
Exemple #5
0
        public static IEnumerable <SearchItem> Sum(SearchExpressionContext c)
        {
            var    skipCount = 0;
            string selector  = null;

            if (c.args[0].types.HasFlag(SearchExpressionType.Selector))
            {
                skipCount++;
                selector = c.args[0].innerText.ToString();
            }

            return(c.ForEachArgument(skipCount).AggregateResults(0d,
                                                                 (context, item, sum) => Aggregate(item, selector, sum, (d, _sum) => _sum + d),
                                                                 (context, sum) => EvaluatorUtils.CreateItem(sum, context.ResolveAlias("Sum"))));
        }