Skip to content

Instantly share code, notes, and snippets.

private static IEnumerable<UnionVisitor<TQuerySource>.UnionExpression> GetUnionExpressions(Expression expression)
{
Func<Expression, IEnumerable<UnionVisitor<TQuerySource>.UnionExpression>> getVisitorExpressions = x =>
{
var unionVisitor = new UnionVisitor<TQuerySource>();
unionVisitor.Visit(x);
return unionVisitor.Expressions;
};
return GetUnionExpressions(getVisitorExpressions(expression), getVisitorExpressions);
}
let GetUnionExpressions<'a> expression =
let getVisitorExpressions expression =
let unionVisitor = new UnionVisitor<'a>()
unionVisitor.Visit(expression : Expression) |> ignore
unionVisitor.Expressions
let rec getUnionExpressions (expressions : seq<UnionVisitor<'a>.UnionExpression>) =
if expressions |> Seq.isEmpty then Seq.empty
else expressions
public abstract class ExpressionVisitorBase<TState>
{
public class ExpressionNotSupportedException : Exception
{
public ExpressionNotSupportedException(Expression expression) :
base(string.Format("Expression {0} not supported.", expression.GetType().Name)) { }
}
public virtual TState Visit(Expression node)
{
"Sometimes the only way you can feel good about yourself is by making someone else look bad. And I'm tired of making other people feel good about themselves." ~Homer J Simpson
require "fileutils"
module FileSystem
def FileSystem.EnsurePath(path)
if !Dir.exists?(path) then
FileUtils.mkdir_p(path)
end
end
public static class MaybeMonadicExtensions
{
public static Maybe<T> ToMaybe<T>(this T value)
{
return value == null ? Maybe.Nothing<T>() : Maybe.Just(value);
}
public static Maybe<B> Bind<A, B>(this Maybe<A> a, Func<A, Maybe<B>> func)
{
return a.HasValue ? func(a.Value) : Maybe.Nothing<B>();
// http://stevesmithblog.com/blog/how-do-i-use-structuremap-with-asp-net-mvc-3/
return _container.GetAllInstances<object>().Where(s => s.GetType() == serviceType);
// Isn't this a better way to do it?
return _container.GetAllInstances(serviceType).Cast<object>();
// Some function
Func<int, int, int, int> multiplyThree = (a, b, c) => a * b * c;
// Lets curry it by breaking it up into multiple functions that only have one arg
Func<int, Func<int, Func<int, int>>> multiplyThreeCurried = (a) => (b) => (c) => multiplyThree(a, b, c);
// This is curried, yay!
var result = multiplyThreeCurried(10)(2)(3);
// This is partially applied
public static IEnumerable<object> GetAllInstances(this IContainer container, IEnumerable<Type> types)
{
return container.Model.AllInstances.
Join(types, x => x.PluginType, y => y, (x, y) => x).
Select(x => x.ConcreteType).
Distinct().
Select(container.GetInstance);
}
// This one liner:
return GetState().Map(x => x.ContainsKey(key) ? x[key] : null);
// Instead of this multiliner
var state = GetState();
return state.ContainsKey(key) ? state[key] : null;