Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created October 14, 2011 10:28
Show Gist options
  • Save chgeuer/1286768 to your computer and use it in GitHub Desktop.
Save chgeuer/1286768 to your computer and use it in GitHub Desktop.
Currying
using System;
using System.Net;
namespace curry
{
class Program
{
static void Main(string[] args)
{
var url = "http://www.microsoft.com/";
Func<string, string> d = Download;
Console.WriteLine(d(url));
Console.WriteLine(d.Partial(url).Retry(5));
Console.WriteLine((d.Curry()(url)).Retry(5));
var d2 = d.Curry();
Console.WriteLine(d2(url).Retry(5));
dynamic C = new Program();
C.Foo = "jk";
Console.WriteLine(C.Foo);
}
private static string Download(string url)
{
return (new WebClient()).DownloadString(url);
}
}
public static class Extensions
{
public static T Retry<T>(this Func<T> func, int retries)
{
for (int i = 0; i < retries; i++)
{
try
{
return func();
}
catch (Exception)
{
}
}
return default(T);
}
public static Func<TResult> Partial<TParameter, TResult>(this Func<TParameter, TResult> func, TParameter param)
{
return () => func(param);
}
public static Func<TParameter, Func<TResult>> Curry<TParameter, TResult>(this Func<TParameter, TResult> func)
{
return param => (() => func(param));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment