Reimplementing LINQ to Objects: Part 9 – SelectMany

The next operator we’ll implement is actually the most important in the whole of LINQ. Most (all?) of the other operators returning sequences can be implemented via SelectMany. We’ll have a look at that at the end of this post, but let’s implement it first. What is it? SelectMany has 4 overloads, which look gradually more and more scary: public static IEnumerable<TResult> SelectMany<TSource, TResult>(     this IEnumerable<TSource> source,     Func<TSource, IEnumerable<TResult>> selector) public static IEnumerable<TResult> SelectMany<TSource, TResult>(     this IEnumerable<TSource> source,     Func<TSource, int, IEnumerable<TResult>> selector) public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(     this IEnumerable<TSource> source,     Func<TSource, IEnumerable<TCollection>> collectionSelector,     Func<TSource, TCollection, … Continue reading Reimplementing LINQ to Objects: Part 9 – SelectMany

Reimplementing LINQ to Objects: Part 8 – Concat

After our quick visit to scalar return types with Count and LongCount, we’re back to an operator returning a sequence: Concat. What is it? Concat only has a single signature, which makes life simple: public static IEnumerable<TSource> Concat<TSource>(     this IEnumerable<TSource> first,     IEnumerable<TSource> second) The return value is simply a sequence containing the elements of the first sequence followed by the elements of the second sequence – the concatenation of the two sequences. I sometimes think it’s a pity that there aren’t Prepend/Append methods which do the same thing but for a single extra element – this would be quite … Continue reading Reimplementing LINQ to Objects: Part 8 – Concat

Reimplementing LINQ to Objects: Part 7 – Count and LongCount

Today’s post covers two operators in one, because they’re so incredibly similar… to the point cut and paste of implementation, merely changing the name, return type, and a couple of variables. What are they? Count and LongCount each have two overloads: one with a predicate, and one without. Here are all four signatures: public static int Count<TSource>(     this IEnumerable<TSource> source) public static int Count<TSource>(     this IEnumerable<TSource> source,     Func<TSource, bool> predicate) public static long LongCount<TSource>(     this IEnumerable<TSource> source) public static long LongCount<TSource>(     this IEnumerable<TSource> source,     Func<TSource, bool> predicate) As you can see, the LongCount signatures are identical to Count except in terms of … Continue reading Reimplementing LINQ to Objects: Part 7 – Count and LongCount

Reimplementing LINQ to Objects: Part 6 – Repeat

A trivial method next, with even less to talk about than "Empty"… "Repeat". This blog post is merely a matter of completeness. What is it? "Repeat" is a static, generic non-extension method with a single overload: public static IEnumerable<TResult> Repeat<TResult>(     TResult element,     int count) It simple returns a sequence which contains the specified element, repeated "count" times. The only argument validation is that "count" has to be non-negative. What are we going to test? There’s really not a lot to test here. I’ve thought of 4 different scenarios: A vanilla "repeat a string 3 times" sequence An empty sequence … Continue reading Reimplementing LINQ to Objects: Part 6 – Repeat

Reimplementing LINQ to Objects: Part 5 – Empty

Continuing with the non-extension methods, it’s time for possibly the simplest LINQ operator around: "Empty". What is it? "Empty" is a generic, static method with just a single signature and no parameters: public static IEnumerable<TResult> Empty<TResult>() It returns an empty sequence of the appropriate type. That’s all it does. There’s only one bit of interesting behaviour: Empty is documented to cache an empty sequence. In other words, it returns a reference to the same empty sequence every time you call it (for the same type argument, of course). What are we going to test? There are really only two things we … Continue reading Reimplementing LINQ to Objects: Part 5 – Empty

Reimplementing LINQ to Objects: Part 4 – Range

This will be a short post, and there’ll probably be some more short ones coming up too. I think it makes sense to only cover multiple operators in a single post where they’re really similar. (Count and LongCount spring to mind.) I’m in your hands though – if you would prefer "chunkier" posts, please say so in the comments. This post will deal with the Range generation operator. What is it? Range only has a single signature: public static IEnumerable<int> Range(     int start,     int count) Unlike most of LINQ, this isn’t an extension method – it’s a plain old … Continue reading Reimplementing LINQ to Objects: Part 4 – Range

Reimplementing LINQ to Objects: Part 3 – "Select" (and a rename…)

It’s been a long time since I wrote part 1 and part 2 of this blog series, but hopefully things will move a bit more quickly now. The main step forward is that the project now has a source repository on Google Code instead of just being a zip file on each blog post. I had to give the project a title at that point, and I’ve chosen Edulinq, hopefully for obvious reasons. I’ve changed the namespaces etc in the code, and the blog tag for the series is now Edulinq too. Anyway, enough of the preamble… let’s get on … Continue reading Reimplementing LINQ to Objects: Part 3 – "Select" (and a rename…)

A Model/View to a Kill (Naked came the null delegate, part 5)

(I suggest you read the earlier parts of the story first. I’m not claiming it’ll make any more sense afterwards, mind you.) Even though Seymour Sharpton’s brain was in a spinlock, a low-level interrupt brought him out of his stupor – namely, an enormous motorcycle bursting through the floor near the daemon. It was impossible to tell the form of the rider under the leather and helmet. When the biker spoke, the voice was digitally disguised but its authority was clear: "Sharpton. Here, now. The rest of you: you know me. Follow us, and there’ll be trouble." Algol hissed sharply, … Continue reading A Model/View to a Kill (Naked came the null delegate, part 5)

Creative freedom, control, and the balance of power

Stephen Colebourne’s comment on my last blog post (adding 1 month -1 day to January 29th) have knocked me for six. To avoid thinking about how I might implement the preferred behaviour in Noda Time while still using Joda Time’s "engine" I’ve decided to write about something else which has been niggling at me. For a long time, I’ve espoused the idea of "design for inheritance or prohibit it" – in other words, default to sealing classes and making methods non-virtual unless you have a good reason to do otherwise. I’ve usually attributed this phrase to Josh Bloch writing in … Continue reading Creative freedom, control, and the balance of power

The joys of date/time arithmetic

(Cross-posted to my main blog and the Noda Time blog, in the hope that the overall topic is still of interest to those who aren’t terribly interested in Noda Time per se.) I’ve been looking at the "period" part of Noda Time recently, trying to redesign the API to simplify it somewhat. This part of the API is what we use to answer questions such as: What will the date be in 14 days? How many hours are there between now and my next birthday? How many years, months and days have I been alive for? I’ve been taking a … Continue reading The joys of date/time arithmetic