Posts Tagged ‘design-patterns’

My check null

December 20, 2011

There are many solutions on the net for avoiding the null check. Here is mine:

public interface Option<T> { }

public struct Nothing<T> : Option<T> { }

public struct Just<T> : Option<T>
{
    public readonly T Value;

    public Just(T value) { Value = value; }
}

public static class OptionExtentions
{
    public static Option<T> ToOption<T>(this T value)
    {
        return (value == null)
            ? new Nothing<T>() as Option<T>
            : new Just<T>(value);
    }

    public static U Select<T, U>(this Option<T> option,
      Func<T, U> func)
    {
        return option.Select(func, () => default(U));
    }

    public static U Select<T, U>(this Option<T> option, 
      Func<T, U> whereJust, Func<U> whereNothing)
    {
        return option is Just<T>
            ? whereJust(((Just<T>)option).Value)
            : whereNothing();
    }
}

And implementation:

return (from h in HttpContext.Current.ToOption()
       select h.User);

Patterns and idioms in functional languages

June 15, 2011

General reference in fp patterns and idioms.

 more

C9 Lectures: Greg Meredith – Monadic Design Patterns for the Web

September 28, 2010

Greg Meredith, a mathematician and computer scientist, has graciously agreed to do a C9 lecture series covering monadic design principles applied to web development. You’ve met Greg before in a Whiteboard jam session with Brian Beckman. The fundamental concept is here is the monad.

more