// Listing 3.17 Adding and multiplying list elements (C#) static int AggregateList(FuncList<int> list, int init, Func<int, int, int> op) { if (list.IsEmpty) return init; // Return initial value for empty list else { // Branch for a non-empty list int rest = AggregateList(list.Tail, init, op); return op(rest, list.Head); } }
public static FuncList[] ParseFuncList(string url) { HtmlWeb webClient = new HtmlWeb(); HtmlDocument doc = webClient.Load(url); List<FuncList> funcList = new List<FuncList>(); string xpath = "//h1[@class='funcName']"; HtmlNode node = doc.DocumentNode.SelectSingleNode(xpath); string funcName = node.InnerText.Trim(); xpath = "//h2[@class='subcat']"; HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes(xpath); int i = 0; foreach (HtmlNode aNode in nodes) { FuncList list = new FuncList(); list.funcName = funcName; list.subcat = aNode.InnerText.Trim(); funcList.Add(list); HtmlNodeCollection nodes1 = aNode.NextSibling.SelectNodes("tr/td[1]/a"); List<string> funcs = new List<string>(); for (int j = 0; j < nodes1.Count; j++) { string name = nodes1[j].InnerText.Trim(); if (name.StartsWith("functx:")) funcs.Add(name.Replace("functx:", "").ToLower()); } list.funclist = funcs.ToArray(); i++; } doc = null; node = null; webClient = null; return funcList.ToArray(); }
/// <summary> /// 操作员登录 /// </summary> /// <param name="OperName"></param> /// <param name="PWD"></param> public void OperLogin(string OperName, string PWD) { alOperFunc = new ArrayList(); oper = new Oper(); try { conn = ConnectionPool.BorrowConnection(); //trans = conn.BeginTransaction(); //安全比对 DataTable dtOper = SqlHelper.ExecuteDataTable(conn, CommandType.Text, "select * from tbOper where cnvcOperName = '" + OperName + "' and cnvcPwd = '" + PWD + "'"); if (null == dtOper) { throw new BusinessException("操作员登录", "操作员名称或密码错误!"); } if (dtOper.Rows.Count == 0) { throw new BusinessException("操作员登录", "操作员名称或密码错误!"); } oper = new Oper(dtOper); //操作员功能列表 if (oper.cnnDeptID != 0) //=0为系统管理员 { //系统管理员具有所有权限 //否则比对权限列表 DataTable dtOperFunc = SqlHelper.ExecuteDataTable(conn, CommandType.Text, "select * from tbOperFunc where cnnOperID = " + oper.cnnOperID); foreach (DataRow dr in dtOperFunc.Rows) { FuncList funcList = new FuncList(dr); alOperFunc.Add(funcList.cnvcFuncCode); } if (alOperFunc.Count == 0) { throw new BusinessException("操作员登录", "操作员无任何权限!"); } } if (oper.cnnDeptID == 0) { iDiscount = 0; } else { Dept dept = new Dept(); dept.cnnDeptID = oper.cnnDeptID; Dept oldDept = EntityMapping.Get(dept, conn) as Dept; iDiscount = oldDept.cnnDiscount; } //折扣上限 //trans.Commit(); } catch (BusinessException bex) //业务异常 { //LogAdapter.WriteBusinessException(bex); throw new BusinessException(bex.Type, bex.Message); } catch (SqlException sex) //数据库异常 { //事务回滚 //trans.Rollback(); //LogAdapter.WriteDatabaseException(sex); throw new BusinessException("数据库异常", sex.Message); } catch (Exception ex) //其他异常 { //事务回滚 //trans.Rollback(); //LogAdapter.WriteFeaturesException(ex); throw new BusinessException("其它异常", ex.Message); } finally { ConnectionPool.ReturnConnection(conn); } }
public static void LinqTellIntSelectTest() { var rws = from env in Rws.GetEnvironment <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>() from s in Rws.Get <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>() from _ in Rws.Put <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>(s + 1) from __ in Rws.Tell <Dictionary <int, string>, IList <int>, Monoid.ListAppendImmutableMonoid <int>, int>(FuncList.Make(28)) select env[s] + 2; var dict = new Dictionary <int, string> { { 5, "e" } }; var(res, state, w) = rws.Run(dict, 5); Assert.Equal("e2", res); Assert.Equal(6, state); Assert.Equal(new int[] { 28 }, w); }
public void WitherThenTest() { var lensOuter = L.Make.Withering <FuncList <FuncList <string> >, FuncList <FuncList <int> >, FuncList <string>, FuncList <int> >(); var lensInner = L.Make.Withering <FuncList <string>, FuncList <int>, string, int>(); var lens = lensOuter.Then(lensInner); var actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make <FuncList <string> >()).ToMaybe(); Assert.Empty(actual.Value()); actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make(FuncList.Make <string>())).ToMaybe(); Assert.Single(actual.Value()); Assert.Equal(new int[] { }, actual.Value()[0]); actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make(FuncList.Make("a", "bb", "cccc", "", "gg"), FuncList.Make <string>(), FuncList.Make("hhhh"))).ToMaybe(); Assert.Equal(3, actual.Value().Count); Assert.Equal(new int[] { 2, 4, 0, 2 }, actual.Value()[0]); Assert.Equal(new int[] { }, actual.Value()[1]); Assert.Equal(new int[] { 4 }, actual.Value()[2]); }
public void WitherTest() { var lens = L.Make.Withering <FuncList <string>, FuncList <int>, string, int>(); var actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make <string>()).ToMaybe(); Assert.Equal(new int[] { }, actual.Value()); actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make("a")).ToMaybe(); Assert.Equal(new int[] { }, actual.Value()); actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make("bb")).ToMaybe(); Assert.Equal(new int[] { 2 }, actual.Value()); actual = L.TraverseMaybe(lens, typeof(Maybe <int>), s => Maybe.JustIf(s.Length % 2 == 0, () => s.Length), FuncList.Make("a", "bb", "cccc", "", "gg")).ToMaybe(); Assert.Equal(new int[] { 2, 4, 0, 2 }, actual.Value()); }
public void TraverseThenTest() { var outer = L.Make.Traversal <FuncList <FuncList <int> >, FuncList <FuncList <string> >, FuncList <int>, FuncList <string> >(); var inner = L.Make.Traversal <FuncList <int>, FuncList <string>, int, string>(); var combined = outer.Then(inner); var actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.Just(i + ""), FuncList.Make <FuncList <int> >()).ToMaybe(); Assert.Empty(actual.Value()); actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.Just(i + ""), FuncList.Make(FuncList.Make <int>())).ToMaybe(); Assert.Single(actual.Value()); Assert.Empty(actual.Value()[0]); actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.JustIf(i % 2 == 0, () => i + ""), FuncList.Make(FuncList.Make(0, 2, 4, 6), FuncList.Make(10, 12))).ToMaybe(); Assert.Equal(2, actual.Value().Count); Assert.Equal(new string[] { "0", "2", "4", "6" }, actual.Value()[0]); Assert.Equal(new string[] { "10", "12" }, actual.Value()[1]); actual = L.Traverse(combined, typeof(Maybe <string>), i => Maybe.JustIf(i % 2 == 0, () => i + ""), FuncList.Make(FuncList.Make(0, 1, 2, 4, 6), FuncList.Make(10, 12))).ToMaybe(); Assert.False(actual.HasValue); }
/// <summary> /// Equivalent to <see cref="IMonad{TSource}"/>, but restricted to <see cref="FuncList{T}"/>. Offers LINQ query support with multiple <c>from</c>-clauses. /// </summary> /// <typeparam name="TSource">The type of the source's value.</typeparam> /// <typeparam name="TMiddle">The type of the selector's result.</typeparam> /// <typeparam name="TResult">The type of the result's value.</typeparam> /// <param name="source">The source.</param> /// <param name="f">The function to apply.</param> /// <param name="resultSelector">The result-selector.</param> public static FuncList <TResult> SelectMany <TSource, TMiddle, TResult> (this FuncList <TSource> source, Func <TSource, FuncList <TMiddle> > f, Func <TSource, TMiddle, TResult> resultSelector) => (FuncList <TResult>)source.Bind(x => (FuncList <TResult>)f(x).Map(y => resultSelector(x, y)));
/// <summary> /// Equivalent to <see cref="IFunctor{TSource}.Map{TResult}(Func{TSource, TResult})"/>, but restricted to <see cref="FuncList{T}"/>. Offers LINQ query support with one <c>from</c>-clause. /// </summary> /// <typeparam name="TSource">The type of the source's value.</typeparam> /// <typeparam name="TResult">The type of the result's value.</typeparam> /// <param name="source">The source.</param> /// <param name="f">The function to apply.</param> public static FuncList <TResult> Select <TSource, TResult>(this FuncList <TSource> source, Func <TSource, TResult> f) => (FuncList <TResult>)source.Map(f);
// ------------------------------------------------------------------------ // Section 3.3.4: Functional list processing // Listing 3.15 Sum of list elements (C#) static int SumList(FuncList<int> numbers) { if (numbers.IsEmpty) // Sum of empty list is 0 return 0; else // A branch for a cons cell return numbers.Head + SumList(numbers.Tail); }
// ------------------------------------------------------------------------ // Section 3.4.1 Processing lists of numbers int MultiplyList(FuncList<int> numbers) { if (numbers.IsEmpty) return 1; else return numbers.Head * MultiplyList(numbers.Tail); }
public SplitPart(Orientation orientation, FuncList <DocumentPart> parts) { this.Orientation = orientation; this.Parts = parts; }
private bool InitModule(ref byte[] Data) { int A; int B; int C; var bCode = new byte[16]; var ms = new MemoryStream(Data); var br = new BinaryReader(ms); Marshal.Copy(Data, 0, new IntPtr(m_Mod), Data.Length); br.BaseStream.Position = 0x18L; C = br.ReadInt32(); B = 1 - C; br.BaseStream.Position = 0x14L; if (B > br.ReadInt32()) { return(false); } br.BaseStream.Position = 0x10L; A = br.ReadInt32(); br.BaseStream.Position = A + B * 4; A = br.ReadInt32() + m_Mod; InitPointer = A; Console.WriteLine("Initialize Function is mapped at 0x{0:X}", InitPointer); SendPacketD = SendPacket; CheckModuleD = CheckModule; ModuleLoadD = ModuleLoad; AllocateMemD = AllocateMem; FreeMemoryD = FreeMemory; SetRC4DataD = SetRC4Data; GetRC4DataD = GetRC4Data; myFunctionList = new FuncList { fpSendPacket = Marshal.GetFunctionPointerForDelegate(SendPacketD).ToInt32(), fpCheckModule = Marshal.GetFunctionPointerForDelegate(CheckModuleD).ToInt32(), fpLoadModule = Marshal.GetFunctionPointerForDelegate(ModuleLoadD).ToInt32(), fpAllocateMemory = Marshal.GetFunctionPointerForDelegate(AllocateMemD).ToInt32(), fpReleaseMemory = Marshal.GetFunctionPointerForDelegate(FreeMemoryD).ToInt32(), fpSetRC4Data = Marshal.GetFunctionPointerForDelegate(SetRC4DataD).ToInt32(), fpGetRC4Data = Marshal.GetFunctionPointerForDelegate(GetRC4DataD).ToInt32() }; Console.WriteLine("Imports: "); Console.WriteLine(" SendPacket: 0x{0:X}", myFunctionList.fpSendPacket); Console.WriteLine(" CheckModule: 0x{0:X}", myFunctionList.fpCheckModule); Console.WriteLine(" LoadModule: 0x{0:X}", myFunctionList.fpLoadModule); Console.WriteLine(" AllocateMemory: 0x{0:X}", myFunctionList.fpAllocateMemory); Console.WriteLine(" ReleaseMemory: 0x{0:X}", myFunctionList.fpReleaseMemory); Console.WriteLine(" SetRC4Data: 0x{0:X}", myFunctionList.fpSetRC4Data); Console.WriteLine(" GetRC4Data: 0x{0:X}", myFunctionList.fpGetRC4Data); // http://forum.valhallalegends.com/index.php?topic=17758.0 myFuncList = new IntPtr(malloc(0x1C)); Marshal.StructureToPtr(myFunctionList, myFuncList, false); pFuncList = myFuncList.ToInt32(); int localVarPtr() { object argobj = pFuncList; var ret = VarPtr(ref argobj); return(ret); } ppFuncList = localVarPtr(); Console.WriteLine("Initializing module"); init = (InitializeModule)Marshal.GetDelegateForFunctionPointer(new IntPtr(InitPointer), typeof(InitializeModule)); m_ModMem = init.Invoke(ppFuncList); pWardenList = Marshal.ReadInt32(new IntPtr(m_ModMem)); myWardenList = (WardenFuncList)Marshal.PtrToStructure(new IntPtr(pWardenList), typeof(WardenFuncList)); Console.WriteLine("Exports:"); Console.WriteLine(" GenerateRC4Keys: 0x{0:X}", myWardenList.fpGenerateRC4Keys); Console.WriteLine(" Unload: 0x{0:X}", myWardenList.fpUnload); Console.WriteLine(" PacketHandler: 0x{0:X}", myWardenList.fpPacketHandler); Console.WriteLine(" Tick: 0x{0:X}", myWardenList.fpTick); GenerateRC4Keys = (GenerateRC4KeysDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpGenerateRC4Keys), typeof(GenerateRC4KeysDelegate)); UnloadModule = (UnloadModuleDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpUnload), typeof(UnloadModuleDelegate)); PacketHandler = (PacketHandlerDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpPacketHandler), typeof(PacketHandlerDelegate)); Tick = (TickDelegate)Marshal.GetDelegateForFunctionPointer(new IntPtr(myWardenList.fpTick), typeof(TickDelegate)); ms.Close(); ms.Dispose(); ms = null; br = null; return(true); }
static void AggregateMain() { // Initialize a sample list var list = FuncList.Cons(1, FuncList.Cons(2, FuncList.Cons(3, FuncList.Cons(4, FuncList.Cons(5, FuncList.Empty <int>()))))); // Multiply and sum elements // the first call prints 15, the second 120 Console.WriteLine(AggregateList(list, 0, Add)); Console.WriteLine(AggregateList(list, 1, Mul)); }
static void ListsMain() { // Create a list storing 1,2,3,4,5 var list = FuncList.Cons(1, FuncList.Cons(2, FuncList.Cons(3, FuncList.Cons(4, FuncList.Cons(5, FuncList.Empty <int>()))))); // Calculates and prints "15" int sum = SumList(list); Console.WriteLine(sum); }