Functional programming with Linq – Enumerable.OfType

Of all the methods available on the Enumerable class, OfType<T> is arguably one of the most useful and yet under utilized method.

For example, you have a list of Cat and Dog objects, all inheriting from a common Animal class, but occasionally you want to perform operations on only the dogs or cats, and that’s where OfType can come in handy:

public class Animal
{
    public string Name { get; set; }
}
public class Dog : Animal {}
public class Cat : Animal {}
…

var animals = new Animal[] { new Cat { Name = "Jess" },
                             new Cat { Name = "Tad" },
                             new Dog { Name = "Bob" } };
// get the dogs from the array of animals, functional style
var dogs = animals.OfType<Dog>();

// get the cats from the array of animals, true imperative style
var cats = new List<Cat>();
foreach (var animal in animals)
{
    if (animal is Cat)
        cats.Add(animal as Cat);
}

Of course, you could equally have filtered the array like this:

// lambda expression syntax
var cats = animals.Where(a => a is Cat).Select(a => a as Cat);
// query expression syntax
var cats = from a in animals where a is Cat select a as Cat;

But using OfType is far cleaner and expressive of your intent.

NHibernate 2.1.0 GA with LINQ to NHibernate and FluentNHibernate

I ran into a versioning problem when I tried to use the latest version of FluentNHibernate with the latest version of LINQ to NHibernate because LINQ to NHibernate is built using a later version of the NHibernate binaries.

After doing a little search, I found a version of NHibernate which both LINQ to NHibernate and FluentNHibernate had been built against here.

So now, I can enjoy the same fluent interface to configure NHibernate and the use of LINQ query like this:

var onePiece = from s in session.Linq<Series>() where s.Name == "One Piece" select s;

Functional programming with Linq – Enumerable.Range

In my Learning F# posts you probably saw how you can generate an int array like this:

let x = [1 .. 10];;

this returns 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

This is pretty cool, and fortunately, you have something very similarly in C# with Enumerable.Range which you can use by supplying a starting number and a count:

var x = Enumerable.Range(1, 10);

Rx framework – IObservable<T>.Retry

As Wes Dyer put very elegantly:

Retry is to Repeat as Catch is to Concat

Ok ok, I know that’s not an exact quote, but I’m sure this is what he intended to say in the first instance :-P

Anyhow, the IObservable<T>.Retry method has all the same features of its Repeat sister method, in that:

1. You can invoke it as extension method:

var zs = xs.Retry(3);

or as static method:

var zs = Observable.Retry(xs, 3);

2. If you don’t give it a count, it’ll retry indefinitely

3. xs.Retry(1) subscribes to xs only once, and xs.Retry(0) will not subscribe to xs at all.

Only this time, the repetition of the observable collection only happens on exception. So, if you take the illustration from the previous post and swap the colours around and you will arrive at a pretty accurate illustration of how Retry works:

image

Rx framework – IObservable<T>.Repeat

Having looked at a number of extension methods in Rx which allows you to combine two observable collections in some way, namely:

There are also extension methods which allow you to repeatedly subscribe to the same observable collection, the suitably name IObservable<T>.Repeat method.

If the repeated observable collection is pre-determined, i.e. regardless of when you subscribe to the observable it’ll always return the same set of values (such as Observable.Return(1)), then those same values will be repeated in the resulting observable collection. This type of observable collections are referred to as Cold Observables, the enumeration equivalent of these will be an array whose values have been defined upfront.

Conversely, if the observable collection’s values are determined at the point of subscription, then when the observable collection is repeated it might yield a different set of values (e.g. when you use the Observable.Defer method to generate an observable collection). This type of observable collections are referred to as Hot Observables, the enumeration equivalent of these will be an Iterator which uses yield return to return whatever value it has at the point of execution.

You can call Repeat either as an extension method:

var zs = xs.Repeat(3);

or as a static method:

var zs = Observable.Repeat(xs, 3);

There are a few things you should keep in mind when you’re using the Repeat method though.

1. If you call Repeat method with no argument, it’ll repeatedly subscribe to the same observable collection indefinitely

2. When you supply the Repeat method with a repeat count, the count actually means how many times in total the observable collection will be subscribed. I.e. xs.Repeat(1) subscribes to xs once, and xs.Repeat(0) will not subscribe to xs at all.

3. If an exception is thrown during any repetition of the observable collection, the resulting observable collection excepts too as illustrated below:

image