Tagged D programming language

One of the most distinctive features of Common Lisp and Lisp in general, are its code-generation and code-manipulation capabilities.

Probably the best example is the LOOP macro - a Swiss Army knife of iteration that can do pretty much anything. The following snippet iterates a list of random numbers collecting some statistics of its contents and does that while being very concise and readable:

(let ((random (loop with max = 500
                    for i from 0 to max
                    collect (random max))))
  (loop for i in random
          counting (evenp i) into evens
          counting (oddp i) into odds
          summing i into total
          maximizing i into max
          minimizing i into min
        finally (format t "Stats: ~A"
                          (list min max total evens odds))))
Stats: (0 499 120808 261 240)

Continue reading

So, I finally found some time for a Template Metaranting follow-up post. This time let's get down to business as this one contains a fair amount of code.

Sadly, I won't rant as much but instead I'll try to show how awesome D's templates really are. We'll write a piece of code, based on this Scheme implementation, that is, a simple monad) that we'll use to build a binary tree, with uniquely numbered nodes containing their height, without any global state (therefore purely) entirely at compile time.

Quick Reader, grab my code!


Continue reading

Here's another post in the series, this time it's C++ vs. the D programming language.

Let's talk about templates. If you've ever tried templates in C++ you surely as hell recall the pages and PAGES of compiler errors and seemingly random placement of typename keyword. Trust me, there are EVEN WORSE problems with templates in C++... Consider the following:

struct Foo {
  template<int N>
  void bar() {}

  template<int N>
  struct Bar {};

template<typename T>
void f() {
  T foo;
  foo.template bar<0>();         // Line 12
  typename T::template Bar<0> b; // Line 13

int main() {

Continue reading

There's an increasing interest with the D programming language amongst my readers so I figured I'll post a bunch of short posts about D and see what happens.

Anyway, here's a classic example showing Ruby's capabilities taken from Seven Languages in Seven Weeks:

class Roman
  def self.method_missing name, *args
    roman = name.to_s
    roman.gsub!("IV", "IIII")
    roman.gsub!("IX", "VIIII")
    roman.gsub!("XL", "XXXX")
    roman.gsub!("XC", "LXXXX")

    (roman.count("I") +
     roman.count("V") * 5 +
     roman.count("X") * 10 +
     roman.count("L") * 50 +
     roman.count("C") * 100)

puts Roman.X
puts Roman.XC
puts Roman.XII

Continue reading

Hello fellow programmers. Look at your favourite programming language.

Now back to Lisp.

Now back to your favourite language.

Now back to Lisp.

Sadly, it isn't Lisp, but if you stopped wasting your time, it could feel like it's Lisp.

Look down.

Back up.

Where are you?

Reading in OldSpice-guy's voice as I introduce the ASM programming language.

What's on your mind?

Back at me.

ASM has it. The expressiveness and brevity augmented by powerful and extensible features.

Look again.

ASM is now usable.

Anything is possible when your programming language is functional and not imperative.

It's highly munctional.


Continue reading

Just to evangelize D a little and increase my code/crap ratio, let's pretend we develop a library in C++ that contains this class:

class SomeMetaVariables {
    std::string foo;
    bool bar;

// ... Somewhere in the client code:
SomeMetaVariables baz;
baz.foo = "foo";
baz.bar = true;

Our library is quite successful and many people are using SomeMetaVariables despite its obvious flaws. Now, say we get many requests for additional functionality, for example: "Make bar true only when foo is set to "foo" and other way arround." "Well, ok." - we say and commit this new version of SomeMetaVariables:

class SomeMetaVariables {
    std::string foo;
    bool bar;

    std::string getFoo() {
        return foo;

    std::string setFoo(std::string newFoo) {
        foo = newFoo;
        bar = (foo == "foo");
        return foo;

    bool getBar() {
        return bar;

    bool setBar(bool newBar) {
        bar = newBar;
        foo = bar ? "foo" : "";
        return bar;

We implemented the requested feature, but SomeMetaVariables' interface has changed... "But why are you mad clients? You asked for it!" - cries the C++ developer.

Continue reading

I didn't make it on time with (car '(3)) and it's been quite a while without any updates, so let's have a quicky with a little tool I've made lately. The assignment was to load a part of the OpenStreetMap and give the good ol' Dijkstra a spin in it, pretty simple yet quite fun.

I figured it would look nicer with some sweet GUI so... I didn't make any. Instead, I went with some minimal OpenGL window and a buncha' lines and squares. Still cool, right? I called it City Blueprints and you can get it here (the page has been deleted since I graduated). Some screenshots follow.

Simple graph of a small town:


Continue reading

It's been a while since my last post, more than I'd wish for actually, and that is because I was hunting a man by the name Iain Buclaw on the IRC. I had to make sure he was ok with me using some of his work in this next project/post (and he obviously is, oh the kind gentleman he is).

Last time I shared some thoughts on latmab, a simple algebraic calculator, I have... commited and todays topic is somewhat related. It's a continuation I'd say, but let's go on with the story...

Continue reading

So, I'm going to break the usual design-quirk rutine and actually show something fun this time and then we'll return to some OOD stuff I was planning to post.

It all started with an assignment me and my brethren freshmen got on our programming subject that would be loosely translated to /Programming languages and design methods/ I believe. We had to code a rather simple algebraics calculator that uses infix notation and supports several math functions such as factorial, power et cetera. The more advanced version of this assignment had to implement integrals and plotting so I thought "why the hell not?". Obviously I didn't aim for the second version as it implied a rather advanced framework that would support all kinds of goodies, because I can't stand writting a piece of crap that's only purpose is to work past the rating tests, and such a framework, the fun experience it might be, would take lot's and lot's of time to become of a satisfying elegance level. Yeah, I am picky, you don't even imagine. Anyway there it was, although I wasn't quite satisfied with it, keeping in mind all this "advanced" and "framework" stuff, it turned out quite... we'll see.

Continue reading

Let's suppose we have this class hierarchy:


It's a fairly specialized class hierarchy providing some functionality, let's say for the sake of this example that SomeClass provides a kind of player-game interaction interface and HierarchyWith defines it as a trigger based interaction (as in "a player does something - it triggers a response"). Our ExampleClass represents an entity that can be triggered by the player and that generates some response and in addition to that is further specialised to have some concrete functionality - let's say it's a lever that a player determined enough can pull to flood something with delicious magma. As we clearly can see ExampleClass is well-defined in terms of OOP, it's highly specialised and modular, but as an in-game entity it lacks a rather crucial functionality - it can't even be displayed on the screen. What do we do now?

Continue reading

Testing in programming is pretty important, no doubts about it, too bad that some languages make it difficult to test the code easly. For instance C++ is pretty limited in terms of DbC and unit testing. All it ever supplied to programmers was a little =assert()= macro that stopped the program providing minimal info about the error and the place it was "caught" in. I mean there are exceptions in C++ and they are pretty usefull, but still it's not enough and requires lots of effort in lerning and implementing correctly. One can still say there are libraries such as CppUnit for those purposes, but we all know how it goes - the more I need to do to get some minor advantage the less I'm interrested in it, and unit testing is so underestimated it's not considered an advantage to have a working piece of code anymore, how about that? And speaking of DbC in C++... It is possible... bam (A sound of a thousand readers going "whaaa" this instant.)

Continue reading