Created
January 5, 2013 05:23
-
-
Save gogsbread/4459925 to your computer and use it in GitHub Desktop.
A poor mock of function pointers in c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define DEBUG | |
using System; | |
using System.Diagnostics; | |
namespace RandomMusings | |
{ | |
public class FunctionPointers | |
{ | |
public static Func<int,int[],int> Summer(Func<int,int,int> sumFunction) | |
{ | |
Func<int,int[],int> MoreSum = (x,args) => | |
{ | |
if(args.Length == 0) | |
return x; | |
else | |
{ | |
int[] newArgs = new int[args.Length - 1]; | |
for(int i= 1; i < args.Length ; i++) | |
newArgs[i - 1] = args[i]; | |
return sumFunction(x,MoreSum(args[0],newArgs)) ; | |
} | |
}; | |
return MoreSum; | |
} | |
public static int Sum (int x, int y) | |
{ | |
return (x + y); | |
} | |
static void Main () | |
{ | |
ConsoleTraceListener listener = new ConsoleTraceListener (); | |
Debug.Listeners.Add (listener); | |
var sum = Summer(Sum); | |
Console.WriteLine(sum(4,new int[]{5,3,4,5,6,3,4,5})); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment