Useful LINQ Operators
While LINQ may not be breaking news, it's a wonderful technology that has changed the way I program, yielding clearer and more concise code than before. There are, however, some common operations that I find useful that aren't part of the standard library. Here, for your use, are some operators that I find myself writing frequently. May they serve you well!
Partition
This operator takes a collection and splits it into "windows" (i.e. subgroups) of a given size. This is useful any time you want to do something in batches.
Cycle
Infinite sequences didn't appear so useful at first, but after a brief struggle I've adopted them in many places. Recently, for example, I needed to dispatch a large number of clients to a small number of servers in a round-robin fashion. By creating an infinite sequence out of the server list, I could .Zip it with the list of clients and - voila! - load was distributed.
Interleave
Not used as often, but this one crops up with enough frequency that I wouldn't feel right excluding it. Interleave alternates between two sequences, yielding first from one sequence and then from the other. It takes two additional arguments - counts of items to be yielded from each sequence in turn. You can create a simple alternation by calling one.Interleave(another, 1, 1), or specify different
The Joys of Invention
I'm sure every developer can relate to this - after mulling over some problem or another, a flash of inspiration strikes - you've just stumbled on something truly elegant! Not only will it help with the problem at hand, but your solution could be useful in many places, and even to many people. Excitedly, you put your code in Github (or whatever the flavor du jour might be), and Google around "just to see". That's when it happens - someone has had the exact inspiration that you've had, and probably years ago.
Today I had yet another one of those moments.
Admittedly, the notion of memoizing an expensive computation is approximately as old as dirt; what I thought was novel was the application of memoization to the lazy computation sequence of IEnumerable<T> in a container that was itself an IEnumerable<T>, something I'd never seen before in .NET. A few moments of blissful coding later, I had something to be proud of:
After the fact, of course, tweaking my initial Google query yielded this gem from the ever-amazing Bart de Smet. A quick perusal of his source code reveals a functionally-identical memoizing enumerable that is superior to what I came up with in almost every way, most notably in that it appears to support multiple concurrent enumerations, something that I hadn't even attempted.
The first reaction for me is always a great deflation - my novel contribution isn't anything of the sort. Following the deflation, though, is the realization that while maybe memoizing enumerables have been done before, someone as smart as Mr. de Smet thought they were a good idea, too. That kind of validation can be almost as nice.
Hello world!
Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!