Being Fluent, Fluid and Functional with Monads.net
Today's project, monads.net by Sergey Zwezdin, is brought to us by a post from Ryan Skurkis.
Do you really like writing If after If after If? && after && after || after ||? Do you really like writing that same event method? Do you really like writing all that usual exception handling code? Empty catches?
Like the thought of continuing to incorporate fluent, fluid and functional programing into your coding practices?
Or maybe you just like the thought of saying "monad?"
Functional concepts have worked their way into C# most notably through linq and lambda expressions, but the benefits of functional concepts in C# don’t have to stop there. This post will exemplify how functional concepts can be used to manage boilerplate code, such as if/else statements, null checks, and logging. The examples below are written using extension methods from the GitHub project, monads.net. monads.net isn’t required or even endorsed for working more functional goodness into your C# code, but it’s a great tool to start learning and sparking ideas with.
Before we dive into the code pit, please read my painless, oversimplified explanation of what monads are:
Monads are utility functions that accept and conditionally pass the result of functionX() to functionY().
Let’s start by reaching into a deeply nested object structure while doing null checks along the way.
What if we don’t care about storing the phone number in a variable. What if instead we just want to dial the number if it’s found.
Cool. If we find a manager and the manager is at work and we have the managers telephone number, we’re dialing it. But what if we want to log to a server or file along the way? An elegant way to handle this is to create our own logging “Monad,” which is another chainable extension method that can be used anywhere in our logic chain.
Our Log() looks like this. It will log the current value and a message if the current value isn’t null. This method will always return the value passed into it even if that value is null. This allows the chaining to continue similar to With(), Do(), and If().
Monads for .NET is helpers for C# which makes easier every day of your developer life. Now supports .NET 3.5-4.0, Silverlight 3-5 and Windows Phone 7.
In functional programming, a monad is a programming structure that represents computations. Monads are a kind of abstract data type constructor that encapsulate program logic instead of data in the domain model. A defined monad allows the programmer to chain actions together to build a pipeline to process data in various steps, in which each action is decorated with additional processing rules provided by the monad. Programs written in functional style can make use of monads to structure procedures that include sequenced operations, or to define some arbitrary control flows (like handling concurrency, continuations, side effects such as input/output, or exceptions).
- Checking exception type (via Type)
- Checking exception type (via predicate)
- Ignore exceptions
Want to know more about monads?
- A functor is a data type that implements the
- An applicative is a data type that implements the
- A monad is a data type that implements the
Maybeimplements all three, so it is a functor, an applicative, and a monad.
What is the difference between the three?
- functors: you apply a function to a wrapped value using
- applicatives: you apply a wrapped function to a wrapped value using
- monads: you apply a function that returns a wrapped value, to a wrapped value using
So, dear friend (I think we are friends by this point), I think we both agree that monads are easy and a SMART IDEA(tm). Now that you’ve wet your whistle on this guide, why not pull a Mel Gibson and grab the whole bottle. Check out LYAH’s section on Monads. There’s a lot of things I’ve glossed over because Miran does a great job going in-depth with this stuff.
Now, you've seen them in action, you've seen what's behind them and, by now, I'm sure you've seen how these can make your coding life that much easier...
This conversation has been locked by the site admins. No new comments can be made.