First, take a look at this code. Take it in… feel it… be it.
1: public class Identity<T>
2: {
3: public T Value { get; private set; }
4: public Identity(T value) { this.Value = value; }
5: }
6:
7: public static class IdentityExtensions
8: {
9: public static Identity<T> ToIdentity<T>(this T value)
10: {
11: return new Identity<T>(value);
12: }
13:
14: public static Identity<V> SelectMany<T, U, V>
15: (this Identity<T> id, Func<T, Identity<U>> k, Func<T, U, V> s)
16: {
17: return s(id.Value, k(id.Value).Value).ToIdentity();
18: }
19: public static Identity<U> SelectMany<T, U>
20: (this Identity<T> id, Func<T, Identity<U>> k)
21: {
22: return k(id.Value);
23: }
24: }
The code below exercises the classes above, so from it you should get a general idea of what’s going on, if you hadn’t already a clue.
1: class Program
2: {
3:
4: static void Main(string[] args)
5: {
6:
7: var r = 5.ToIdentity()
8: .SelectMany(x => 6.ToIdentity(), (x, y) => x + y);
9:
10: var b = from x in 5.ToIdentity()
11: from y in 6.ToIdentity()
12: select x + y;
13:
14: Console.WriteLine(r.Value);
15: Console.WriteLine(b.Value);
16:
17: Console.ReadKey();
18:
19: }
20: }
This code frightens me; not terrifically, but enough to keep me honest and humble. As it were, I see only code; I don’t see the blonde, brunette or the redhead. I can discern the LINQ, extension methods, lambda expressions and generics, but there is obviously something bigger happening…. magic, if you will. Now I don’t believe in magic, but I am certainly willing to call it that if it saves me from the spiraling rabbit hole that an educational journey often becomes. And I’ll admit, LINQ has always been magic.
Now you might be asking yourself what all this has to do with Reactive Extensions (Rx). If so, then great! This isn’t pointless. If you are unfamiliar with Rx, follow the link… I don’t regurgitate. Better yet, go to Erik Meijer’s intro, which should pique your interest. If you are familiar with Rx and have explored the Hands on Lab, then you clearly didn’t follow the rabbit on page 8.
The code represents monads, which are apparently the theory behind LINQ, which is fundamental to Rx. In fact, some suggest that LINQ should have been called Language INtegrated Monads. I wish it were. My mind always wants to draw a direct correlation to query in the SQL sense, but Query in the LINQ sense has more to do with functional programming. I extracted this code from Wes Dyer’s blog entry on The Marvels of Monads If monads are new to you, or you don’t get the fundamentals of LINQ, I highly recommend following through the steps in the article; don’t just cut and paste my reduction. if you’re hoping for an explanation of Monads, I don’t think it can be done, as Wikipediahas so generously proven; bring some Excedrin along for the journey. I think it is just something you have to ‘get’, and I think Wes’ article presents it in such a manner.
The journey from Rx to monads included many more resources; I’ve reference only the ones that I believe helped me gain a fundamental understanding of LINQ. The others were just static.