···3131 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
3232 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
3333 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</s:String>
3434+ <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=LocalFunctions/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
3435 <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue"><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></s:String>
3536 <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=3d1afed0_002D8eeb_002D476b_002Db81f_002D68998a8855fe/@EntryIndexedValue"><Policy><Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Private members"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /><Kind Name="METHOD" /><Kind Name="PROPERTY" /><Kind Name="EVENT" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /></Policy></s:String>
3637 <s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=a0ea5b45_002Df3e8_002D489b_002Da1bf_002D6fe14030968c/@EntryIndexedValue"><Policy><Descriptor Staticness="Static, Instance" AccessRightKinds="Internal" Description="Internal methods and properties"><ElementKinds><Kind Name="METHOD" /><Kind Name="PROPERTY" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="_" Suffix="" Style="AaBb" /></Policy></s:String>
+165-165
Prexonite/Commands/List/FoldL.cs
···11-// Prexonite
22-//
33-// Copyright (c) 2014, Christian Klauser
44-// All rights reserved.
55-//
66-// Redistribution and use in source and binary forms, with or without modification,
77-// are permitted provided that the following conditions are met:
88-//
99-// Redistributions of source code must retain the above copyright notice,
1010-// this list of conditions and the following disclaimer.
1111-// Redistributions in binary form must reproduce the above copyright notice,
1212-// this list of conditions and the following disclaimer in the
1313-// documentation and/or other materials provided with the distribution.
1414-// The names of the contributors may be used to endorse or
1515-// promote products derived from this software without specific prior written permission.
1616-//
1717-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1818-// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1919-// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2020-// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2121-// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2222-// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2323-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2424-// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2525-// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626-using System;
2727-using System.Collections.Generic;
2828-using Prexonite.Compiler.Cil;
2929-using Prexonite.Types;
3030-3131-namespace Prexonite.Commands.List
3232-{
3333- /// <summary>
3434- /// Implementation of the foldl function.
3535- /// </summary>
3636- /// <remarks>
3737- /// <code>function foldl(ref f, left, source)
3838- /// {
3939- /// foreach(var right in source)
4040- /// left = f(left,right);
4141- /// return left;
4242- /// }</code>
4343- /// </remarks>
4444- public class FoldL : PCommand, ICilCompilerAware
4545- {
4646- #region Singleton
4747-4848- private FoldL()
4949- {
5050- }
5151-5252- private static readonly FoldL _instance = new FoldL();
5353-5454- public static FoldL Instance
5555- {
5656- get { return _instance; }
5757- }
5858-5959- #endregion
6060-6161- public static PValue Run(
6262- StackContext sctx, IIndirectCall f, PValue left, IEnumerable<PValue> source)
6363- {
6464- if (sctx == null)
6565- throw new ArgumentNullException(nameof(sctx));
6666- if (f == null)
6767- throw new ArgumentNullException(nameof(f));
6868- if (left == null)
6969- left = PType.Null.CreatePValue();
7070- if (source == null)
7171- source = new PValue[] {};
7272-7373- foreach (var right in source)
7474- {
7575- left = f.IndirectCall(sctx, new[] {left, right});
7676- }
7777- return left;
7878- }
7979-8080- public static PValue RunStatically(StackContext sctx, PValue[] args)
8181- {
8282- if (sctx == null)
8383- throw new ArgumentNullException(nameof(sctx));
8484- if (args == null)
8585- throw new ArgumentNullException(nameof(args));
8686-8787- //Get f
8888- IIndirectCall f;
8989- if (args.Length < 1)
9090- throw new PrexoniteException("The foldl command requires a function argument.");
9191- else
9292- f = args[0];
9393-9494- //Get left
9595- PValue left;
9696- if (args.Length < 2)
9797- left = null;
9898- else
9999- left = args[1];
100100-101101- //Get the source
102102- IEnumerable<PValue> source;
103103- if (args.Length == 3)
104104- {
105105- var psource = args[2];
106106- source = Map._ToEnumerable(sctx, psource) ?? new[] {psource};
107107- }
108108- else
109109- {
110110- var lstsource = new List<PValue>();
111111- for (var i = 1; i < args.Length; i++)
112112- {
113113- var multiple = Map._ToEnumerable(sctx, args[i]);
114114- if (multiple != null)
115115- lstsource.AddRange(multiple);
116116- else
117117- lstsource.Add(args[i]);
118118- }
119119- source = lstsource;
120120- }
121121-122122- return Run(sctx, f, left, source);
123123- }
124124-125125- public override PValue Run(StackContext sctx, PValue[] args)
126126- {
127127- return RunStatically(sctx, args);
128128- }
129129-130130- /// <summary>
131131- /// A flag indicating whether the command acts like a pure function.
132132- /// </summary>
133133- /// <remarks>
134134- /// Pure commands can be applied at compile time.
135135- /// </remarks>
136136- [Obsolete]
137137- public override bool IsPure
138138- {
139139- get { return false; } //indirect call
140140- }
141141-142142- #region ICilCompilerAware Members
143143-144144- /// <summary>
145145- /// Asses qualification and preferences for a certain instruction.
146146- /// </summary>
147147- /// <param name = "ins">The instruction that is about to be compiled.</param>
148148- /// <returns>A set of <see cref = "CompilationFlags" />.</returns>
149149- CompilationFlags ICilCompilerAware.CheckQualification(Instruction ins)
150150- {
151151- return CompilationFlags.PrefersRunStatically;
152152- }
153153-154154- /// <summary>
155155- /// Provides a custom compiler routine for emitting CIL byte code for a specific instruction.
156156- /// </summary>
157157- /// <param name = "state">The compiler state.</param>
158158- /// <param name = "ins">The instruction to compile.</param>
159159- void ICilCompilerAware.ImplementInCil(CompilerState state, Instruction ins)
160160- {
161161- throw new NotSupportedException();
162162- }
163163-164164- #endregion
165165- }
11+// Prexonite
22+//
33+// Copyright (c) 2014, Christian Klauser
44+// All rights reserved.
55+//
66+// Redistribution and use in source and binary forms, with or without modification,
77+// are permitted provided that the following conditions are met:
88+//
99+// Redistributions of source code must retain the above copyright notice,
1010+// this list of conditions and the following disclaimer.
1111+// Redistributions in binary form must reproduce the above copyright notice,
1212+// this list of conditions and the following disclaimer in the
1313+// documentation and/or other materials provided with the distribution.
1414+// The names of the contributors may be used to endorse or
1515+// promote products derived from this software without specific prior written permission.
1616+//
1717+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1818+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1919+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2020+// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2121+// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2222+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2323+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2424+// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2525+// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626+using System;
2727+using System.Collections.Generic;
2828+using Prexonite.Compiler.Cil;
2929+using Prexonite.Types;
3030+3131+namespace Prexonite.Commands.List
3232+{
3333+ /// <summary>
3434+ /// Implementation of the foldl function.
3535+ /// </summary>
3636+ /// <remarks>
3737+ /// <code>function foldl(ref f, left, source)
3838+ /// {
3939+ /// foreach(var right in source)
4040+ /// left = f(left,right);
4141+ /// return left;
4242+ /// }</code>
4343+ /// </remarks>
4444+ public class FoldL : PCommand, ICilCompilerAware
4545+ {
4646+ #region Singleton
4747+4848+ private FoldL()
4949+ {
5050+ }
5151+5252+ private static readonly FoldL _instance = new FoldL();
5353+5454+ public static FoldL Instance
5555+ {
5656+ get { return _instance; }
5757+ }
5858+5959+ #endregion
6060+6161+ public static PValue Run(
6262+ StackContext sctx, IIndirectCall f, PValue left, IEnumerable<PValue> source)
6363+ {
6464+ if (sctx == null)
6565+ throw new ArgumentNullException(nameof(sctx));
6666+ if (f == null)
6767+ throw new ArgumentNullException(nameof(f));
6868+ if (left == null)
6969+ left = PType.Null.CreatePValue();
7070+ if (source == null)
7171+ source = new PValue[] {};
7272+7373+ foreach (var right in source)
7474+ {
7575+ left = f.IndirectCall(sctx, new[] {left, right});
7676+ }
7777+ return left;
7878+ }
7979+8080+ public static PValue RunStatically(StackContext sctx, PValue[] args)
8181+ {
8282+ if (sctx == null)
8383+ throw new ArgumentNullException(nameof(sctx));
8484+ if (args == null)
8585+ throw new ArgumentNullException(nameof(args));
8686+8787+ //Get f
8888+ IIndirectCall f;
8989+ if (args.Length < 1)
9090+ throw new PrexoniteException("The foldl command requires a function argument.");
9191+ else
9292+ f = args[0];
9393+9494+ //Get left
9595+ PValue left;
9696+ if (args.Length < 2)
9797+ left = null;
9898+ else
9999+ left = args[1];
100100+101101+ //Get the source
102102+ IEnumerable<PValue> source;
103103+ if (args.Length == 3)
104104+ {
105105+ var psource = args[2];
106106+ source = Map._ToEnumerable(sctx, psource) ?? new[] {psource};
107107+ }
108108+ else
109109+ {
110110+ var lstsource = new List<PValue>();
111111+ for (var i = 2; i < args.Length; i++)
112112+ {
113113+ var multiple = Map._ToEnumerable(sctx, args[i]);
114114+ if (multiple != null)
115115+ lstsource.AddRange(multiple);
116116+ else
117117+ lstsource.Add(args[i]);
118118+ }
119119+ source = lstsource;
120120+ }
121121+122122+ return Run(sctx, f, left, source);
123123+ }
124124+125125+ public override PValue Run(StackContext sctx, PValue[] args)
126126+ {
127127+ return RunStatically(sctx, args);
128128+ }
129129+130130+ /// <summary>
131131+ /// A flag indicating whether the command acts like a pure function.
132132+ /// </summary>
133133+ /// <remarks>
134134+ /// Pure commands can be applied at compile time.
135135+ /// </remarks>
136136+ [Obsolete]
137137+ public override bool IsPure
138138+ {
139139+ get { return false; } //indirect call
140140+ }
141141+142142+ #region ICilCompilerAware Members
143143+144144+ /// <summary>
145145+ /// Asses qualification and preferences for a certain instruction.
146146+ /// </summary>
147147+ /// <param name = "ins">The instruction that is about to be compiled.</param>
148148+ /// <returns>A set of <see cref = "CompilationFlags" />.</returns>
149149+ CompilationFlags ICilCompilerAware.CheckQualification(Instruction ins)
150150+ {
151151+ return CompilationFlags.PrefersRunStatically;
152152+ }
153153+154154+ /// <summary>
155155+ /// Provides a custom compiler routine for emitting CIL byte code for a specific instruction.
156156+ /// </summary>
157157+ /// <param name = "state">The compiler state.</param>
158158+ /// <param name = "ins">The instruction to compile.</param>
159159+ void ICilCompilerAware.ImplementInCil(CompilerState state, Instruction ins)
160160+ {
161161+ throw new NotSupportedException();
162162+ }
163163+164164+ #endregion
165165+ }
166166}