Example #1
0
File: Sum.cs Project: SealedSun/prx
        public static PValue RunStatically(StackContext sctx, PValue[] args)
        {
            if (args == null)
                throw new ArgumentNullException("args");
            if (sctx == null)
                throw new ArgumentNullException("sctx");

            //let sum xs acc = Seq.foldl (fun a b -> a + b) acc xs

            PValue acc;
            IEnumerable<PValue> xsArgs;

            if (args.Length == 0)
                return PType.Null;

            if (args.Length == 1)
            {
                acc = PType.Null;
                xsArgs = args;
            }
            else
            {
                acc = args[args.Length - 1];
                xsArgs = args.Take(args.Length - 1);
            }

            var xss = xsArgs.Select(e => Map._ToEnumerable(sctx, e)).Where(e => e != null);

            foreach (var xs in xss)
                foreach (var x in xs)
                    acc = acc.Addition(sctx, x);

            return acc;
        }