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()); }
private static Result Sum(Evaluator.Args args) { return(args .Flatten() .Select(obj => args.ToNumber(obj)) .Sum()); }
private static Result Avg(Evaluator.Args args) { return((long)args .Flatten() .Select(obj => args.ToNumber(obj)) .Average()); }
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()); }
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))); }
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()); }
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()); } }
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()); } }
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()); }
private static Result Count(Evaluator.Args args) { return(args.Flatten().Count()); }
private static Result Head(Evaluator.Args args) { var numElements = (int)args.GetNumSwitch("n", 10); return(args.Flatten().Take(numElements).ToArray()); }
private static Result StrCat(Evaluator.Args args, string defaultSeparator) { return(string.Join( args.GetStrSwitch("d", defaultSeparator), args.Flatten().Select(args.Preview))); }