Skip to content

Instantly share code, notes, and snippets.

@jeremiahredekop
Last active July 21, 2017 17:50
Show Gist options
  • Select an option

  • Save jeremiahredekop/86b89831e65c7f305585 to your computer and use it in GitHub Desktop.

Select an option

Save jeremiahredekop/86b89831e65c7f305585 to your computer and use it in GitHub Desktop.

Revisions

  1. jeremiahredekop revised this gist Jul 2, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Sample.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    //Adapter production usage which parses an NLog log level input and returns a custom enum
    private static CustomLogLevel GetCustomLevel(LogLevel input)
    {
    return input.ToString()
    .Pipe(str => Enum.Parse(typeof (SdLogLevel), str))
    .Pipe(o => (CustomLogLevel) o);

    // OR:
    //return (CustomLogLevel) Enum.Parse(typeof(CustomLogLevel), input.ToString());
    }
  2. jeremiahredekop created this gist Feb 10, 2015.
    40 changes: 40 additions & 0 deletions PipingExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    public static class PipingExtensions
    {
    /// <summary>
    /// Take an object, pipe it into a function, and return the result.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="T2"></typeparam>
    /// <param name="obj"></param>
    /// <param name="f"></param>
    /// <returns></returns>
    public static T2 Pipe<T, T2>(this T obj, Func<T, T2> f)
    {
    return f(obj);
    }

    /// <summary>
    /// Pipes object it into a function, and returns object.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <param name="f"></param>
    /// <remarks>useful for encapsulating procedural void method calls</remarks>
    public static T PipeKeep<T>(this T obj, Action<T> f)
    {
    f(obj);
    return obj;
    }

    /// <summary>
    /// Pipes object into a void function (action)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <param name="f"></param>
    /// <remarks>for cases where the output is to be ignored</remarks>
    public static void PipeVoid<T>(this T obj, Action<T> f)
    {
    f(obj);
    }
    }