示例#1
0
        private static Result Cut(Evaluator.Args args)
        {
            var separator   = args.ToString(args.GetSwitch("d") ?? " \t\r\n");
            var fieldsValue = args.GetSwitch("f");
            var fields      = fieldsValue != null
                ? args.Preview(args.ToScalar(fieldsValue))
                : "1";

            var fieldIndices = new HashSet <int>(fields
                                                 .Split(',')
                                                 .SelectMany(f => int.TryParse(f, out var idx) ? new[] { idx - 1 } : new int[0]));

            return(args
                   .Flatten()
                   .Select(obj => string
                           .Join(
                               separator,
                               args
                               .Preview(obj)
                               .Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                               .Where((_, idx) => fieldIndices.Contains(idx)))
                           .Trim())
                   .Where(s => s.Length > 0)
                   .ToArray());
        }
示例#2
0
 private static Result Sum(Evaluator.Args args)
 {
     return(args
            .Flatten()
            .Select(obj => args.ToNumber(obj))
            .Sum());
 }
示例#3
0
 private static Result Avg(Evaluator.Args args)
 {
     return((long)args
            .Flatten()
            .Select(obj => args.ToNumber(obj))
            .Average());
 }
示例#4
0
        private static Result Tail(Evaluator.Args args)
        {
            var numElements = (int)args.GetNumSwitch("n", 10);
            var flattened   = args.Flatten().ToList();

            return(flattened.Skip(flattened.Count - numElements).ToArray());
        }
示例#5
0
        private static Result Join(Evaluator.Args args)
        {
            var separator = args.GetSwitch("d");

            return(string.Join(
                       separator != null ? args.ToString(separator) : Environment.NewLine,
                       args.Flatten().Select(args.Preview)));
        }
示例#6
0
        private static Result Sort(Evaluator.Args args)
        {
            var objs = args.Flatten();

            var ordered = args.HasSwitch("n") // numeric sorting (otherwise string sorting)
                ? objs.OrderBy(args.ToInt)
                : objs.OrderBy(args.Preview);

            var finalOrder = args.HasSwitch("r") // reverse
                ? ordered.Reverse()
                : ordered;

            return(finalOrder.ToArray());
        }
示例#7
0
        private static Result Uniq(Evaluator.Args args)
        {
            var groups = args.Flatten().GroupBy(obj => args.Preview(obj));

            if (args.HasSwitch("c")) // count objects in each group
            {
                return(groups
                       .Select(grp => $"{grp.Count()}\t{args.Preview(grp.First())}")
                       .ToArray());
            }
            else
            {
                return(groups
                       .Select(grp => grp.First())
                       .ToArray());
            }
        }
示例#8
0
        private static Result Uniq(Evaluator.Args args)
        {
            var fieldToGroupBy = args.GetStrSwitch("k", null);
            Func <object, object> keySelector = o => o;

            if (!string.IsNullOrEmpty(fieldToGroupBy))
            {
                keySelector = o => args.Eval.Resolve(o).Properties.FirstOrDefault(p => p.Name == fieldToGroupBy)?.Value;
            }

            var aa     = args.Flatten();
            var groups = aa.GroupBy(obj => args.Preview(keySelector(obj)));

            if (args.HasSwitch("c")) // count objects in each group
            {
                return(groups
                       .Select(grp =>
                {
                    return new ObjectInfo(
                        preview: $"{grp.Count()}: {grp.Key}",
                        properties: new[]
                    {
                        new Property(name: "Key", value: grp.Key),
                        new Property(name: "Count", value: grp.Count()),
                        new Property(name: "Elems", value: grp.ToArray())
                    });
                })
                       .ToArray());
            }
            else
            {
                return(groups
                       .Select(grp => grp.First())
                       .ToArray());
            }
        }
示例#9
0
        private static Result Sort(Evaluator.Args args)
        {
            var objs = args.Flatten();

            var fieldToSortBy = args.GetStrSwitch("k", null);
            Func <object, object> keySelector = o => o;

            if (!string.IsNullOrEmpty(fieldToSortBy))
            {
                keySelector = o => args.Eval.Resolve(o).Properties.FirstOrDefault(p => p.Name == fieldToSortBy)?.Value;
            }

            IComparer <object> comparer = args.HasSwitch("n")
                ? Comparer <object> .Create((lhs, rhs) => Comparer <long?> .Default.Compare(args.Eval.TryToNumber(lhs), args.Eval.TryToNumber(rhs)))
                : Comparer <object> .Create((lhs, rhs) => Comparer <string> .Default.Compare(args.Preview(lhs), args.Preview(rhs)));

            var ordered = objs.OrderBy(keySelector, comparer);

            var finalOrder = args.HasSwitch("r") // reverse
                ? ordered.Reverse()
                : ordered;

            return(finalOrder.ToArray());
        }
示例#10
0
 private static Result Count(Evaluator.Args args)
 {
     return(args.Flatten().Count());
 }
示例#11
0
        private static Result Head(Evaluator.Args args)
        {
            var numElements = (int)args.GetNumSwitch("n", 10);

            return(args.Flatten().Take(numElements).ToArray());
        }
示例#12
0
 private static Result StrCat(Evaluator.Args args, string defaultSeparator)
 {
     return(string.Join(
                args.GetStrSwitch("d", defaultSeparator),
                args.Flatten().Select(args.Preview)));
 }