Skip to content

Instantly share code, notes, and snippets.

@linuxbender
Created December 6, 2012 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxbender/4222715 to your computer and use it in GitHub Desktop.
Save linuxbender/4222715 to your computer and use it in GitHub Desktop.
FuncDemo
//
// FuncDemos
//
// Extensens.cs - 06.12.2012
//
// Author: glenn
using System;
using System.Net;
namespace FuncDemos
{
public static class Extensens
{
public static T WithRetry<T>(this Func<T> action)
{
var result = default(T);
int retryCount = 0;
bool succees = false;
do
{
try
{
result = action();
succees = true;
}
catch (WebException ex)
{
retryCount++;
}
} while (retryCount < 3 && !succees);
return result;
}
//
// partial app
public static Func<TResult> Partial<TParam1, TResult>(this Func<TParam1, TResult> func, TParam1 parameter)
{
return () => func(parameter);
}
//
// cuury
// take func with N-Parameter and return func with N-Paramter - 1
public static Func<TParam1, Func<TResult>> Curry<TParam1, TResult>( this Func<TParam1, TResult> func)
{
return parameter => () => func(parameter) ;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace FuncDemos
{
class Program
{
static void Main(string[] args)
{
var output = 0;
Func<int, bool> isNUll = i => ((i*i)%2) == 0;
Func<string, Func<int, bool>, bool> isValid = (i, func) => int.TryParse(i, out output) && !func(int.Parse(i));
var zahl = "3";
var isTrue = isValid(zahl, isNUll);
Console.WriteLine("value is : "+ isTrue);
//
// webClinet demo
var client = new WebClient();
Func<string, string> download = url => client.DownloadString(url);
Func<string, Func<string>> downloadCurry = download.Curry();
var data = download.Partial("http://wwww.20min.ch").WithRetry();
var data2 = downloadCurry("http://www.20min.ch").WithRetry();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment