Skip to content

Instantly share code, notes, and snippets.

@klumsy
Created September 30, 2014 20:28
Show Gist options
  • Save klumsy/1e29b3bc6cc69fe9c8c3 to your computer and use it in GitHub Desktop.
Save klumsy/1e29b3bc6cc69fe9c8c3 to your computer and use it in GitHub Desktop.
random experimental extension methods
public static class Using
{
public static T Expression<T, T1>(Func<T1> createResourceLamda, Func<T1,T> work)
{
//what using statement generates in IL is equiv to this pattern
//http://msdn.microsoft.com/en-us/library/yh598w02.aspx
T1 usingResource = createResourceLamda();
try
{
return work(usingResource);
}
finally
{
if (usingResource != null)
((IDisposable)usingResource).Dispose();
}
}
}
public static class TryCatch
{
public static T Expression<T>(Func<T> lamda, Action<Exception> onException)
{
try
{
return lamda();
}
catch(Exception e)
{
onException(e);
return default(T);
}
}
public static T Expression<T, T1>(Func<T> lamda, Action<T1> onException) where T1 : Exception
{
try
{
return lamda();
}
catch (T1 e)
{
onException(e);
return default(T);
}
}
}
public static class Retry
{
public static IEnumerable<T1> Expression<T1>(
int retries,
Func<IEnumerable<T1>> action)
{
if (retries < 1) throw new Exception("must have 1 or more retries");
var iterations = 1;
while (iterations <= retries)
{
try
{
// Debugger.Log(0, "", "processing iteration " + iterations.ToString() + "\r\n");
return action();
}
catch(Exception ex)
{
if (iterations >= retries)
{
throw;
}
iterations++;
}
}
throw new InvalidOperationException("Unexpected code path reached. Algorithmically this should not happen.");
}
public static T1 Expression<T1>(
int retries,
Func<T1> action)
{
var iterations = 1;
while (iterations <= retries)
{
try
{
// Debugger.Log(0, "", " X processing iteration " + iterations.ToString() + "\r\n");
return action();
}
catch (Exception ex)
{
if (iterations >= retries)
{
throw;
}
iterations++;
}
}
throw new Exception("this code path should never happen but the compiler wants it covered.");
}
public static T1 Expression<T1>(
int retries,
Func<T1> action,
Action<Exception,int,string> exceptionProcessor,
string context = ""
)
{
var iterations = 1;
while (iterations <= retries)
{
try
{
//Debugger.Log(0, "", " X processing iteration " + iterations.ToString() + "\r\n");
return action();
}
catch (Exception ex)
{
if (exceptionProcessor != null) exceptionProcessor(ex, iterations,context);
if (iterations >= retries)
{
throw;
}
iterations++;
}
}
throw new Exception("this code path should never happen but the compiler wants it covered.");
}
public static void Action(
int retries,
Action action)
{
var iterations = 1;
while (iterations <= retries)
{
try
{
Debugger.Log(0, "", " X processing iteration " + iterations.ToString() + "\r\n");
action();
}
catch (Exception ex)
{
if (iterations >= retries)
{
throw;
}
iterations++;
}
}
}
}
public static class ext
{
public static IEnumerable<T1> Batch<T, T1>(
this IEnumerable<T> list,
int batchSize,
Func<IEnumerable<T>, IEnumerable<T1>> action,
bool processInParallel = false)
{
int i = 0;
var grpqry =
from item in list
group item by (int)i++ / batchSize
into part
select part.AsEnumerable();
var prlqry = (processInParallel)
? grpqry
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(6)
: grpqry;
return prlqry.SelectMany(x => action(x));
}
public static IEnumerable<T1> Batch2<T, T1>(
this IEnumerable<T> list,
int batchSize,
int maxParallelism,
Func<IEnumerable<T>, IEnumerable<T1>> action)
{
int i = 0;
return (
from item in list
group item by (int)i++ / batchSize
into part
select part.AsEnumerable())
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(maxParallelism).SelectMany(x => action(x));
}
public static IEnumerable<T1> BatchByBatchSize<T, T1>(
this IEnumerable<T> list,
int batchSize,
Func<IEnumerable<T>, IEnumerable<T1>> action)
{
int i = 0;
var grpquery =
(from item in list
group item by (int)i++ / batchSize
into part
select part.ToList()).ToList();
return grpquery
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(grpquery.Count).SelectMany(x => action(x));
}
public static IEnumerable<T1> BatchByMaxParallel<T, T1>(
this IEnumerable<T> list,
int maxParallel,
Func<IEnumerable<T>, IEnumerable<T1>> action)
{
var items = list.ToList();
var batchSize = (int)Math.Ceiling((double) items.Count / maxParallel);
int i = 0;
var grpquery =
(from item in list
group item by (int)i++ / batchSize
into part
select part.ToList()).ToList();
return grpquery
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(grpquery.Count).SelectMany(x => action(x));
}
public static IEnumerable<T1> ParallelForEachExpression<T, T1>(
this IEnumerable<T> list,
int maxParallel,
Func<T, T1> action)
{
//return list.Select(x => action(x));
return list.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(maxParallel)
.Select(x => action(x));
//list.AsParallel().Select(x => action(x));
}
public static IEnumerable<T1> Batch3<T, T1>(
this IEnumerable<T> list,
int batchSize,
Func<IEnumerable<T>, IEnumerable<T1>> action,
bool processInParallel = false)
{
int i = 0;
var grpqry =
(from item in list
group item by (int)i++ / batchSize
into part
select part.AsEnumerable()).SelectMany(x => action(x));
var prlqry = (processInParallel)
? grpqry
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.WithDegreeOfParallelism(50)
: grpqry;
return prlqry;;
}
public static dynamic Union(this object first, params object[] others)
{
var allObjects = new[] { first }.Concat(others);
var allProperties = allObjects.SelectMany(o => o.GetType().GetProperties(),
(o, p) => new { p.Name, Value = p.GetValue(o, null) });
IDictionary<string, object> expando = new ExpandoObject();
foreach (var property in allProperties.Where(p => !expando.ContainsKey(p.Name)))
expando.Add(property.Name, property.Value);
return expando;
}
public static dynamic ToExpando(this object obj)
{
var props = new[] { obj }.SelectMany(x => x.GetType().GetProperties(), (x, y) => new { y.Name, Value = y.GetValue(x, null) });
IDictionary<string, object> expando = new ExpandoObject();
foreach (var prop in props.Where(p => !expando.ContainsKey(p.Name)))
expando.Add(prop.Name, prop.Value);
return expando;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment