Saturday, June 23, 2012

Lazy List in C#

I was playing around with some F# code and thinking about lazy lists in Haskell and how I could get them in F#. Well, since I know how to make an enumerable function in C# I decided to have a go making one in that instead.

void Main()
{
foreach (var i in LazyRange(1, (n) => ++n).Take(10))
{
System.Console.WriteLine(i);
}

System.Console.WriteLine();

var fizz = LazyRange(1, (n) => ++n)
.TakeWhile(n => n < 1000)
.Where(n => n % 3 == 0 || n % 5 == 0)
.Sum();

System.Console.WriteLine(fizz);
}

IEnumerable<T> LazyRange<T>(T start, Func<T, T> successor)
{
T value = start;
while (true)
{
yield return value;
value = successor(value);
}
}

To use it supply a starting value and a function to provide each successive value. Use Take or TakeWhile to limit the collection otherwise you will end up with an infinite list.

Friday, April 6, 2012

Long overdue update

I’ve had a lot going on in the past 18-months. Earthquakes in Christchurch for one, moving to the United Kingdom for another. Still, no real excuse not to be updating my blog, except maybe some programmers block.

While work sometimes sees me going weeks without writing much code, or even days without writing any, I try to keep myself busy with programming projects which I tend to pick up and put down again.

Learning new programming languages is an interest of mine, and one I’ve started using on a regular basis is D. It is a nice language to work in and feels to me to be most like a mix between C++ and C#, both languages I am quite familiar with. The D language seems to be quite mature, and the standard libraries are catching up, but the developer ecosystem isn’t anywhere near the more popular mainstream languages. Tools are a bit lacking right now too. This hasn’t stopped me on embarking on a few projects.

The first is Project Euler. The site contains a list of mathematical problems to solve, with the intention you write a program to do so. Going through these problems got me up to speed with D a lot quicker than just messing around with a tutorial. I haven’t been on in a while but I did get up to 63 problems solved.

The second project goes back to a long running interest of mine in ray tracers. Simply put a ray tracer renders a 3D scene by calculating a ray from a camera to a point on the screen (usually at least one per pixel), and determining what objects in the scene intersect with the ray. Then you perform some calculations based on the vectors at that intersection point to work out what colour it is. My first ray tracer was written about 6 years ago as part of a University assignment and I’ve been hooked ever since. Unfortunately I haven’t worked on a single program for 6 years, I’ve worked on several iterations of the same program over that time not really making much progress. My latest version is in D, and with a couple days of work taking the better C++ code I had and converting it to D I have yet another basic ray tracer. If I do more work on it more in the future it may become a topic for the blog.

More recently I have become interested in creating programming languages. It started with an idea on how you could turn a Reverse Polish Notation calculator into a language. The basic RPN interpreter is a typical student project. I know I made a couple during my study days. A bit of research on the idea lead me to Forth, which I ended up half (or a third) implementing in D. I then got a bit more ambitious and tried to come up with a more complex calculation only to get bogged down in details again. That design process is still on-going though so I might just make something of it yet.

With programming languages on my mind this put me in an appropriate mood to be receptive of the new project of Markus ‘Notch’ Persson, 0x10c. In summary it is an MMORPG set in the distant future in space. The most interesting part of this game is each player’s ship comes with a fully programmable computer, based on the fictional DCPU-16 chip. It is a very simple 16-bit processor and the spec is available so it isn’t surprising that emulators and assemblers are turning up everywhere. I’ve even written an emulator in D over a couple of nights and I’m planning an assembler next. It might even be interesting to implement a simple Pascal compiler for it.