Click here to Skip to main content
15,896,915 members
Articles / Programming Languages / C#

STL Dotnet

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
19 Oct 20052 min read 38.3K   261   28  
STL for C#.
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Stl
{
	public delegate T Generator<T>();
	public delegate T UnaryFunction<Arg, T>(Arg x);
	public delegate T BinaryFunction<Arg1, Arg2, T>(Arg1 x, Arg2 y);
	// public delegate bool Predicate<T>(T obj);
	public delegate bool Relation<T>(T x,T y);

	namespace Functional
	{
		public static class Plus : object
		{
			public static decimal Decimal(decimal x, decimal y)
			{
				return x + y;
			}
			public static double Double(double x, double y)
			{
				return x + y;
			}
			public static float Float(float x, float y)
			{
				return x + y;
			}
			public static long Int64(long x, long y)
			{
				return x + y;
			}
			public static int Int32(int x, int y)
			{
				return x + y;
			}
		}
		public static class Minus : object
		{
			public static decimal Decimal(decimal x, decimal y)
			{
				return x - y;
			}
			public static double Double(double x, double y)
			{
				return x - y;
			}
			public static float Float(float x, float y)
			{
				return x - y;
			}
			public static long Int64(long x, long y)
			{
				return x - y;
			}
			public static int Int32(int x, int y)
			{
				return x - y;
			}
		}
		public static class Multiplies : object
		{
			public static decimal Decimal(decimal x, decimal y)
			{
				return x * y;
			}
			public static double Double(double x, double y)
			{
				return x * y;
			}
			public static float Float(float x, float y)
			{
				return x * y;
			}
			public static long Int64(long x, long y)
			{
				return x * y;
			}
			public static int Int32(int x, int y)
			{
				return x * y;
			}
		}
		public static class Divides : object
		{
			public static decimal Decimal(decimal x, decimal y)
			{
				return x / y;
			}
			public static double Double(double x, double y)
			{
				return x / y;
			}
			public static float Float(float x, float y)
			{
				return x / y;
			}
			public static long Int64(long x, long y)
			{
				return x / y;
			}
			public static int Int32(int x, int y)
			{
				return x / y;
			}
		}
		public static class Modulus : object
		{
			public static decimal Decimal(decimal x, decimal y)
			{
				return x % y;
			}
			public static double Double(double x, double y)
			{
				return x % y;
			}
			public static float Float(float x, float y)
			{
				return x % y;
			}
			public static long Int64(long x, long y)
			{
				return x % y;
			}
			public static int Int32(int x, int y)
			{
				return x % y;
			}
		}
		public static class Negate : object
		{
			public static decimal Decimal(decimal x)
			{
				return -x;
			}
			public static double Double(double x)
			{
				return -x;
			}
			public static float Float(float x)
			{
				return -x;
			}
			public static long Int64(long x)
			{
				return -x;
			}
			public static int Int32(int x)
			{
				return -x;
			}
		}

		public static class Binders : object
		{
			public static UnaryFunction<Arg2, T> Bind1st<Arg1, Arg2, T>(BinaryFunction<Arg1, Arg2, T> f, Arg1 value)
			{
				UnaryFunction<Arg2, T> del = delegate(Arg2 arg2)
				{
					return f(value,arg2);
				};
				
				return del;
			}
			public static UnaryFunction<Arg1, T> Bind2nd<Arg1, Arg2, T>(BinaryFunction<Arg1, Arg2, T> f, Arg2 value)
			{
				UnaryFunction<Arg1, T> del = delegate(Arg1 arg1)
				{
					return f(arg1,value);
				};
				
				return del;
			}
			
			// ça, c'est nouveau, histoire de s'amuser un peu
			public static UnaryFunction<Arg, T> Compose<Arg,R,T>(UnaryFunction<R, T> f, UnaryFunction<Arg, R> g)
			{
				UnaryFunction<Arg, T> fog = delegate(Arg arg)
				{
					return f(g(arg));
				};

				return fog ;
			}
			public static BinaryFunction<Arg1, Arg2h, T> Compose2nd<Arg1,Arg2,Arg2h,T>(BinaryFunction<Arg1, Arg2, T> f, UnaryFunction<Arg2h, Arg2> h)
			{
				BinaryFunction<Arg1, Arg2h, T> composed = delegate(Arg1 x,Arg2h y)
				{
					return f(x,h(y));
				};

				return composed;
			}
			public static BinaryFunction<Arg1h, Arg2, T> Compose1st<Arg1, Arg2, Arg1h, T>(BinaryFunction<Arg1, Arg2, T> f, UnaryFunction<Arg1h, Arg1> h)
			{
				BinaryFunction<Arg1h, Arg2, T> composed = delegate(Arg1h x, Arg2 y)
				{
					return f(h(x), y);
				};

				return composed;
			}
			public static BinaryFunction<Arg2, Arg1, T> Symetric<Arg1, Arg2, T>(BinaryFunction<Arg1, Arg2, T> f)
			{
				BinaryFunction<Arg2, Arg1, T> composed = delegate(Arg2 x, Arg1 y)
				{
					return f(y,x);
				};

				return composed;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
Frédéric DIDIER is a 4 years experienced .Net developper. He has written a sawmill complete critical-time optimization engine in C# (Log-bucking, breakdown, small log processing ,
edger ,trimmer , harvest-parc simulation, short log sorting), improving efficiency and quality of professional existing applications written with MFC, demonstrating the superirity of .Net Framework in designing classes and applications.

Comments and Discussions