Still none for D though

January 18th, 2008

Oh, also, qtHaskell. :yaay: I’ve wanted this for so long (well, since I knew about Qt and Haskell, which I suppose isn’t that long), and, and, it’s exactly what I wanted!

Except for the inexplicable lack of QProgressBar, but let’s not pick.

PS. Advertising :D

Of KConfigDialogs and SIGABRTs

January 18th, 2008

Since KDE4 is actually a real thing now, instead of just something that’ll happen in the future sometime, I don’t really have an excuse not to start writing things for it. So I am.

Holy crap.

I assumed it would be like Qt on steroids (that is, having features for everything you could possibly want), but seriously. KConfigDialog saves settings automatically. You just throw up a dialog, and all else is done.

It may be somewhat apparent that a login box is as far as I’ve got so far.

Anyway, what I’m trying to write is a client for http://pics.livejournal.com, because the current de facto Linux offering is a Perl upload-everything-in-a-directory script. No, really. I haven’t used it, so it may be surprisingly feature-complete, but we must have a GUI client or all the OS X fanbois will laugh at us.

Incidentally, the reason I have a single config box to show for a few hours’ work, other than coming in knowing next-to-nothing about KDE’s architecture, is because KConfigDialog kept asserting false (and SIGABRTing) and refusing to tell me why. #undefing QT_NO_DEBUG and, um, something similar from KDE didn’t make it tell me, so after traipsing through the API and source, I finally figured out it was because I called the submit URL ‘Server’ in the .ui file, and ‘Interface’ in the KConfig files. :suicide:

Prime sieve

December 11th, 2007

Since I haven’t posted in ages, and D doesn’t get nearly enough love from anyone, have a prime sieve.

This is public domain, I suppose. It’s not very spectacular, and took maybe ten minutes to write, so whatever.

module sieve;
 
import fun;
 
long[] sieveTo(long lim)
{
    long l = 0;
    long[] o; o.length = 100;
    foreach (ref i; o) i = 2;
 
    for (long i = 2; i < lim; i++) {
        if (!o.any( (long p) { return i != 0 && i % p == 0; } )) {
            if (l >= o.length) o.extend();
            o[l++] = i;
        }
    }
 
    o.length = l;
    return o;
 
}
 
private void extend(T)(ref T[] arr)
{
    int l = arr.length;
    arr.length = arr.length * 2;
    for (int i = l+1; i < arr.length; ++i)
        arr[i] = 2;
}

the relevant part of fun.d (where I reimplement the parts of Haskell I miss :v ) is the any function:

bool any(T)(in T[] arr, bool delegate(T) p)
{
    foreach(t; arr)
        if (p(t)) return true;
    return false;
}

All in all, D is surprisingly concise, and nice, and people should use it more.

And it should have Qt bindings for it. :(

Automatic method forwarding in Ruby

November 21st, 2007

Warning for googlers: this post is probably not useful. I wasn’t that good at Ruby metaprogramming when I wrote this post. (The obvious followup question is why I wrote it in the first place. I have no idea.)


I’m implementing a Tree in Ruby for an Euler problem (I don’t think I’m quite good enough to attempt it in Haskell yet). Yes I know there’s probably several in the standard libraries, but that’s cheating.

Anyway, a lot of the methods, like size, each, and so on are easiest implemented as recursive methods of the nodes. Meaning they’re all just calling say @root.size, and nothing else. This is not on.

One of the best things about Ruby is that you can just define new methods for any class, at any time, and moreover you can do this automatically using instance_eval and friends. So this was just dying for a forward_to method which takes a list of names, and forwards those methods to some member or other. The first form is thus1:

module ForwardTo
    def forward_to mem, *meths
        meths.each do |meth|
            instance_eval "def #{meth}; @#{mem}.send :#{meth}; end"
        end
    end
end

Obviously this doesn’t allow arguments or blocks to be passed, making all kinds of things impossible, but notably each, which takes both (it has an argument specifying the order of traversal), and was the whole point. Luckily this is easily solved, by adding *args and &block. Also for some reason I forgot about %<> quoting2 the first time around, so this iteration is a lot more readable.

module ForwardTo
    def forward_to mem, meths
        meths.each do |meth|
            instance_eval %<
                def #{meth}(args, &block)
                    @#{mem}.send :#{meth}, *args, &block
                end
            >
        end
    end
end

end end end end end end end end end… Anyway, you probably already know that send doesn’t honour method visibility, so we could be accidentally calling private methods. So I changed it to —

— And then, just a second ago when writing this, I came to my senses and realised that I’m in an eval so I don’t have to mess around with this sort of thing: I can just have @#{mem}.#{meth} in the string instead. Also, I put the method in Object instead because it’s small and unlikely to mess things up, and it saves a call to include.

class Object
    def forward_to mem, meths
        meths.each do |meth|
            instance_eval %<
                def #{meth}(args, &block)
                    @#{mem}.#{meth}(*args, &block)
                end
            >
        end
    end
end

There is still a big problem with this, though. You will probably want to put it in the class body, along with things like attr_accessor:

class Foo
    @mem = []
    # ...
    forward_to :mem, :each, :size
end

… which will not work. The ‘instance’ that instance_eval sees is the instance of Class; in other words, this will add class methods each and size. Which will forward to instance variables. Useful, I know. The workaround I’m using at the moment is to put forward_to calls into initialize. I’ll fix it properly when I find the name of the method that does what I mean.

Update: There’s a not-very-well-hidden method called class_eval, which… does exactly what you’d expect. So you can use that instead of instance_eval and then forward_to can be used in exactly the same way as the attr_* methods. (There’s also a module_eval, incidentally.)


  1. Actually the first iteration was some godawful mess with method_missing, but let’s not go there. 

  2. Which the syntax highlighter can’t cope with, so I’ll not highlight the snippets which use it. 

They forgot M (monadine)

October 30th, 2007

http://www.icfpcontest.org/Endo.pdf

Endo is an alien life form, belonging to the species of the Fuun. Endo needs your help! Earth’s environmental conditions can be harsh for a life form not properly adapted. Endo had the bad luck of being dropped on Earth by an Interstellar Garbage Collector. Both the life form and its faithful space ship Arrow were severely hurt in the crash, and even worse, after leaving the damaged space craft Endo was hit by a cargo container that was also dropped by the Garbage Collector.

Fuun DNA can be represented as a finite sequence consisting of four bases, denoted by the four letters I (infinine), C (continuine), F (functorine), and P (polymorphine).

The puns, they just won’t stop.

Project Euler, bash version

October 28th, 2007

OK, so when I’m bored I sometimes attack a few of the problems on Project Euler; so far I’ve done about 40-odd because I’m slow and inept. I just finished problem 41, which is to find the highest pandigital1 number that is prime. I did my program in Haskell, of course, but in the thread someone had this:

primes 1234567 7654321 | grep 1 | grep 2 | grep 3 \
  | grep 4 | grep 5 | grep 6 | grep 7 | tail -n 1

I don’t think I need to tell you that this is the best answer ever. And it’s far faster than mine…


  1. The Wikipedia definition seems to allow multiple occurrences of the same digit; obviously PE didn’t (otherwise there would be infinitely many and thus no ‘largest’). Also, you were allowed numbers less than 123456789 as long as an n-digit number had all the digits 1–n, for example 4231. Which is good since the answer had seven digits. 

Disk failure

October 24th, 2007

Quoth e2fsck /dev/hda6 (which is /):

File /usr/lib/libkdeinit_kicker.so [...]
  has 1 multiply-claimed block(s), shared with 1 file(s):
        <The bad blocks inode>
Clone multiply-claimed blocks<y>?

Well that certainly sounds, um, happy. :suicide: (Also /var/messages.4 and something to do with git, but bleh old logs and I use darcs.)

Though now I have a bad blocks inode (I’d prefer not to have any bad blocks, but whatever), hopefully my disk will stop dying every ten seconds.

Also, why the hell does Fedora 7 change /dev/hdx for /dev/sdx? It just seems to add confusion for no reason.

No more Windows

October 18th, 2007

Today I decided that there is little need in keeping an OS that I’ve used perhaps an hour or less total in the last year, so I deleted its partition. My heart belongs to Linux. :unsmith:

To remove it I grabbed the GParted LiveCD; it turns out I didn’t need to, but I was going to expand /home to fit the space. But either way it’s something to have if I need it later.

My plan was to delete Windows’ partition, hda2, and expand /home, which is hda3, to fill its space. I was expecting this to be quite involved: reformatting /windows to ext3, copying the contents of /home over, deleting the latter and expanding it, changing the label on the new partition to /home, deleting hda3, and resizing hda2 in its place. But GParted abstracts all that away. It lets you tell it to expand things backwards and it’ll deal with all that itself. So I told it to delete /windows and expand /home to fill the space. I clicked apply and waited.

Deleting the Windows partition was fast, but since all the data had to be moved backwards, resizing home wasn’t, at all. It stopped about halfway through the test (thankfully not the commit), complaining of an I/O error. I have no idea why, but whatever.1

Plan B time: creating an ext3 partition in the place of /windows and call it misc. Which I did, and I now have a 20 GiB empty partition belonging to my normal account. I might in fact do the moving-it-to-/home the hard way, now at least I know my system boots.

Oh, I also had to edit /etc/fstab. Painless, if only because I copied the line above it replacing LABEL=/home with LABEL=/misc (after e2labelling it, obviously). And removed the windows line.

Now I’m off to sudo cp -Rv /home/* /home/.??* /misc. I foresee this taking quite a while.

Edit: I’m back and with the exception of a couple of unimportant files everything seems intact. :yaay: Though I forgot to preserve ownership and home directory /home/andy does not exist is a less-than-nice way of saying it. (You forgot the -p you twonk would have been much better.)

Icon by Alain Clement.


  1. I was somewhat less apathetic about it at the time, of course. Anything that involves moving partitions makes me paranoid, even if I had backed everything up about ten minutes previously. 

Maple

October 17th, 2007

After some annoyance, and coughing up £151, I now have Maple on here to play around with. And to do maths microlab exercises, of course.

As much as the weird syntax (and Swing GUI) is annoying, it’s still fun to play around with. Being able to right-click an equation and integrate, differentiate, solve, or do whatever else to it is the most wonderful thing ever.

Since I don’t really know my way around it2 I can’t really say much about it yet, but yay, new toy.

Image from Wikipedia, taken by this guy. It’s cc-by-sa.


  1. Which is of course better than having to pay about £50, but still. 

  2. There were six microlab sessions last year which took about half an hour each, and that’s the only exposure I’ve had to it. 

Charts! For vowels!

October 16th, 2007

Contrary to (my) assumptions, I actually wrote a program to draw the vowel diagrams like on the IPA chart, instead of the normal “I will! Honest! But later”. Apart from being used to show which symbol means what, the IPA Handbook also uses them to show more accurately what the vowels actually are. This is the intended use of this program, of course, because otherwise you might as well have an image.

Read the rest of this entry »