Older blog entries for mathrick (starting at number 100)

14 Jan 2008 (updated 6 Jul 2008 at 13:46 UTC) »
I am not able to rightly comprehend the kind of confusion of ideas that could provoke such a claim

Update: For some reason WP disabled comments when I first posted this. Fixed now.

There’s a thread on c.l.l going, in which people try to find a reason macros haven’t caught on for the past 30 years, despite their immense usefulness. One of the cited arguments was this statement by one of the men in charge of Java process:

The advantages of Java is that it easily serves as a lingua franca - everyone can read a Java program and understand what is going on. User defined macros destroy that property. Every installation or project can (and will) define its own set of macros, that make their programs unreadable for everyone else. Programming languages are cultural artifacts, and their success (i.e., widespread adoption) is critically dependent on cultural factors as well as technical ones.

My claim is that this is about as true as C++’s approach to performance, in which people copy things all the time, because that’s what manual memory management makes them do. Just consider the “design patterns” omnipresent in Java. What is a macro, if not a structured transformation of source, following certain pattern? Of course, I’m saying nothing new here, but I just stumbled upon a particularly good demonstration of why “macros decrease legibility of code” is utter nonsense. Consider this snippet:

(restart-bind
    ((retry
      (lambda () (throw 'retry nil))
       :report-function
       (lambda (stream)
         (write "Retry reading the record." :stream stream))))
  (catch 'ok
    (loop do
         (catch 'retry
           (throw 'ok
             (unless (read-record)
               (error "Record not available.")))))))

Just try analysing it, how fast can you find out what exactly it does? Aren’t braided catch/throws fun? Now consider the same snippet using a macro:

(with-retry (:report "Retry reading the record.")
  (unless (read-record)
    (error "Record not available.")))

Not only is the code 4x shorter and uses idiomatic naming conventions and code structure, the macro also comes with a docstring and parameters to let you easily influence the working. And most importantly, you only have to understand it once. Whereas in Java every time you encounter a similar-looking snippet, you have to analyse it to see if it’s actually the same thing, or subtly different.

So, to sum up with a checklist, the macroless code is:

  • More verbose
  • Really complex and confusing
  • Hard to spot and identify reliably
  • Not documented
  • Must be re-invented each time you need it
  • Does not ultimately prevent convoluted code from coming up. If your code needs retry functionality, then so it does, and no amount of “ours is a simple language for the masses” can change it. Though if you make it sufficiently painful, people will probably find ways to dillute the apparent complexity amongst modules, only compounding the problems, and/or pretend they don’t really need their program correct, because it’s such a massive PITA to do it.

Now, to be fair, this is not to say that macros can’t be hard — very often they are, because you generally use macros to solve problems that would be even harder without them. You don’t deny surgeons access to endoscopy because they might not be skillfull enough to handle it — instead you make sure they are or have to find another trade. There’s no reason to treat programmers differently.

Saying “macros make code unreadable” is like saying “race bets can only be done on manure-covered floor, or else you won’t know you’re dealing with horses”.

#

Chattr

Admit it, your Web 2.0 experience hasn’t been full up till now, has it? But now, with Chattr here, you can fix that and talk like a real Web 2.0 weenie with no effort. Its full glory can be had here.

Executive summary: yes, it’s an XChat plugin, no, it has no use.

#

;_;
(with-christmas-gifts
  (visit family))
(setf *money* 0)
(print ";_;")

#

Motherfucking Fujitsu Heavy Industries

ghrrrrrrr.Apple pisses me off.

Why? Well, in anticipation of some substantial cash surge (knock on wood), I decided to check in the local (hell yeah, in Odense there’s one) Apple store, to take a look at 12″ iBooks, because that’s exactly what I’d like to get — it’s damn small, has very long battery life, and is sexy (I’m not the only one to share this view, I think, because people with 12″ iBooks are common sight at the Uni). So, I went in, there was one on the display, small as expected, checked the price, OK, within acceptable range, seems they all ship with Tiger these days, which is also good. So the last question was, can I have SuperDrive with this one?

No. That’s only with 14″ ones.

Fuck. Of course, you can have 12″ PowerBook with SuperDrive, so it’s not a technical limitation, it’s just that Apple wants to suck every bit of cash out of you, because prices for PowerBooks are really crazy. It’s 13.500kr vs 8.200kr. And there’s no way I can afford that. But OTOH, I don’t want to get stuck without DVD burner either.

So, does anyone know a good, linux-compatible, 12″ notebook with DVD burner and long-lasting battery, x86 or whatever? And yes, the 12″ bit here really does matter, when you commute to school every day by bike at least.

#

Things and stuff

Lots of everything going on, due to various circumstances I wasn’t able to post in the last week, so the amount of what should be written is rather large. Part of the reason was that I was sick last weekend, so instead of going for the announced cottage trip and having fun, I spent it lying in bed, not-really-able to think or write consistently. After that, I no longer had a computer available, so posting was rather out of question. And now you know.

Anyway, back to the stuff, in random order:

Bought myself a pair of bike lights, to be able to bike at night and not get fines from the local police. Turned out they didn’t work. Gone back to the big store I got it from (Bilka), and they have special section split out reserved just for the customer service. You just get a number in line, wait till it’s your turn, and settle your matters quickly and painlessly. Now, I can’t really compare it to Poland, because I don’t think I ever had to complain about a faulty product, but what I like here is the acceptance of the fact that shit happens, and someone just has to handle it, and its better for the business to do the dirty work, instead of relaying it to the customer. That’s still far too uncommon in Poland, sadly. My belief in the fact it’s a system, not an isolated occurence is strengthened by me having to cancel the cottage trip (due to the abovementioned sickness) just the next day, and it went even smoother than before — I just needed to make one call, I was instantly proposed to get my money back whenever I’m good enough to visit the office. Really a zero fuss system (even though in the end the lights stopped woring after few days, and I spent more on them I’d pay for at local “everything home and workshop” store for proper lights, but the customer service is still nice).

OTOH, I must say that things here are seriously fucked up. Honestly. For example, I missed one lecture because the timetable isn’t up for it yet (thanks to busted electronic everything system, which generated hellish delays all over the place), and I couldn’t login to “blackboard” system on Tuesday (again, busted system), and that was the only place where I could learn that first lecture is on Wednesday. So I learned that, on Wednesday after coming back from school.
Further, you’d expect that all these “e-learn” gizmos we’re supposed to use, SingleSignOn system (from Oracle, no less), account automatically created for every student would mean that I’m able to use the labs computers right from the start. Well, no. Instead I need to register with every institute separately, and of course, each has its own naming policy, acceptable passwords policy, policy on whether I’m regarded to be guest or full time student (which projects on my account’s name), hell, I even need to get Windows and Unix accounts separately in one institute! (But, not in another). Oh, and Unix in form of Solaris 8 is just a joy to use (even with Gnome 2.8 installed), becaus that fucking thing has no support for XKB. Which means no hope for Polish keyboard. Which means no school paper writing. Which means angry me, grrr. Fortunately, both institutes also have Linux labs, which is a lot nicer. Even though it’s Mandrake (or Mandriva now), which in turns means every menu and settings fucked up to infinity. D’oh.

Oh, and another lecture I missed because it overlapped with another class (from the same institute, nothing fancy like classes from different institutes), and, I think I found the Denmark’s single civil officer unable to speak even a word in English. But I wonder why she was placed in Folkeregister (National Register), which by definition deals with assigning CPR (sort of Danish social security evidence) numbers to the foreigners. And everything here is so damn expensive. Coffee is 12kr at the very least (I mean, coffee at the human-run place. Vending machines strangely enough have 4 kr for coffee, strangely because they have ∞ kr for everything else). And the books I’m supposed to buy will easily amount to several thousand kr. Booo :(

#

Welcome party

Just got back from welcome party at Uni. There was rector’s speech, then a speech from one professor researching globalisation, and then there was kindof party. That’s what I call international. We got Polish, Chinese, German, American, Hungarian, Danish, Korean, French, African, Norwegian and some other nationalities I have forgotten. Afterwards we went to the bar in the city centre, and now I’m beat. G’night.

#

Another day, another mishap

So. I wanted to get around the city and look for cheap bikes today. Instead, I got flat tyre and spent entire time I had allocated on first attempting to fix it, and then getting new one and changin it. Ghrr. Also, the laptop I’m using right now has danish keyboard, in small notebookish layout to that. And some genious invented having “Fn” key instead of Ctrl, and Ctrl slightly to the right. So I need to aim to the right when I want to hit Ctrl-Backspace, which often results in Alt-Backspace instead. Which for some reason means “delete everything” in IE (yeah, I’ll be installing FF ASAP here). So first half of this post has been already typed in 3 times. Yay for great ideas. Now I need to finish before I run out of battery juice, because I have the laptop, I have the power adaptor, but I don’t have cable to connect that adaptor to the mains :>

#

Hi there, from Denmark

Wow, an update. There haven’t been one in something like… 4 months? That means I had to delete several thousands of spam comments before doing anything :(. I need to finally update to WP 1.5, hopefully it’ll cope better with spam. Honestly, I’d like to just have my spamassasin but for blog, and a way to delete all the spam comments at once.

Anyway, I’m in Denmark now (been here since Thursday). Thanks to Socrates/Erasmus programme, I’m in Odense (take a look at the coordinates which should be somewhere close to this post :), as an exchange student. So far it’s very cool, if you have ever seen kids books illustrations of typical Danish town, there’s a news for you: it’s all true. The houses really look like they do on pics, most are small and very, hmm, cute. Also, the interior design seems to be universally good, you just don’t get to see badly arranged houses.

The city is all crazy about bikes, Odense being one of the biggest Danish towns is also the most bikified one. You can get by bike everywhere. And you have special bike lanes on every street, including roundabouts, which is something completely uncommon in Poland. That’s the part I really like about it, although I still don’t have a bike on my own (I will be going to town to look for a bike tomorrow). What totally beat me was young woman, with two small kids, doing quite significant shopping, and then riding home by bike. Turns out that with a basket, kids seat and a trailer you can do it.

There’s still a lot to do before the term starts, good thing my “buddy” (the person you get assigned to help you get around in the city when you arrive, sort out the paperwork, etc.) is a very nice guy, and extremely helpful. Thanks to him, I have a bike before I get my own, same thing with a computer (I don’t have a laptop, and couldn’t really take my desktop with me here).

One very noticable thing (besides everyone speaking Danish) is that you can get away with English almost anywhere. Even the old ladies walking with dogs will tell you you don’t need to be afraid in English once they notice you don’t understand Danish :>. Another is that Danes really like their Danebrog, there’s a good chance to see a flag on every street, and about every other shop is called “Dansk something” (the rest is called “Fynsomething”, Fyn being the name of island Odense is situated on). Quite similar to Americans and their love of Stars & Stripes, I think.

That’s enough for today, I need to get up early tomorrow and look around the city for a cheap bike.

#

25 Nov 2007 (updated 5 Dec 2007 at 13:53 UTC) »
More useful bash prompt

Working on a project with a rather deep directory hierarchy, I finally got tired of my prompt overflowing the line and wrapping around. So, here’s a handy <code>bash</code> function to put in your <code>$PS1</code>. It should be noted that I did not write it, I only wrapped it up in a function and added some aesthetic options, but all the hard work and <code>bash</code> hackery was done by BearPerson from <code>#bash</code>.

# Produces the same effect as \w in $PS1, but makes sure that the
# result length doesn't exceed $1 chars. If any dirs are omitted, they
# are replaced with [...]. Optional $2 turns on ANSI VT100 escape
# sequences to dim the [...] part. Optional $3 specifies the colour
# code to dim to (otherwise 02, "dim")
bound_pwd ()
{
    limit=${1:-40}
    ansi=$2
    colour=${3:-2}

    regex="~$|~?/.{1,$limit}$|/[^/]*$"
    pre=$([ $ansi ] && echo -n "\[\033[${colour}m\]")
    suf=$([ $ansi ] && echo -n "\[\033[0m\]")

    CANDIDATE="${PWD/$HOME/~}"
    [[ $CANDIDATE =~ $regex ]]
    [[ $BASH_REMATCH != $CANDIDATE ]] && CANDIDATE="$pre[...]$suf$BASH_REMATCH"

    echo $CANDIDATE
}

To use it, either put it in your <code>.bashrc</code>, or in a file that is sourced by <code>.bashrc</code>, then replace <code>\w</code> in your <code>$PS1</code> with <code>$(bound_pwd)</code>. Personally I use <code>$(bound_pwd 25 1)</code> to get at most 25 chars, with VT100 colour codes.

Obligatory screenshot:

Bash prompt with the shortening

<s>Update: If you use VT100 colours, remember to surround the <code>bound_pwd</code> invocation with <code>\[ \]</code>, otherwise non-printing characters will confuse <code>bash</code> and make it wrap lines incorrectly</s>

Update 2: The above update was wrong. It’s actually more complex than that, and due to the fact that <code>bash</code> doesn’t exactly have coherent escaping semantics, I had to rework the function. If you use VT100 colours and have downloaded an earlier version of bound_pwd, you have to download it again, it has changed. Additionally, to have it really work, you have to add this function:

set_ps1 ()
{
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:'"$(bound_pwd 25 1)"'$ '
}

And in <code>.bashrc</code>, add this:

# Yes, it's silly that ; alone is not valid syntax
PROMPT_COMMAND="${PROMPT_COMMAND:-true};set_ps1"

Make sure it’s the last line to set <code>PROMPT_COMMAND</code>. Especially if you’re on Debian/Ubuntu, as their default <code>.bashrc</code> sets it conditionally.

#

13 Nov 2007 (updated 14 Nov 2007 at 22:22 UTC) »
Routing complete Apache traffic to a CGI handler

Following up on my CGI-Lisp work, here’s a short recipe on how to route the entire Apache traffic to a CGI handler. This is not trivial because of a few problems that need solving:

  1. <code>mod_actions</code> will fall into infinite loop if you try to associate a handler with <code><Location /></code> (as will <code>mod_rewrite</code> if you attempt to rewrite <code>/.*</code>)
  2. <code>mod_rewrite</code> will not execute CGI scripts by default
  3. <code>mod_rewrite</code> only serves physical paths under <code>DocumentRoot</code> (and it’s good practice not to have <code>/cgi-bin/</code> under DocumentRoot)

These can be all solved, but require some searching and reading into the meaning of various options, so I’m posting a ready solution here:

<VirtualHost *:80>
    ...
    DocumentRoot /var/www/
    ...

    RewriteEngine On
    # PT means "passthrough" and will allow mod_rewritten URLs to be matched by
    # virtual locations, not just physical paths
    # T= specifies mime-type to ensure the CGI handler will be executed
    # sock.cgi is the handler we want to handle the entire traffic
    RewriteRule ^/(.*) /cgi-bin/sock.cgi/$1 [PT,T=application/x-httpd-cgi]

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        #Add whatever options you normally use for your /cgi-bin/
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

As an added bonus, it seems that the <code>REQUEST_URI</code> sent is the URL before rewriting, so you don’t have to do anything special to filter out <code>/cgi-bin/sock.cgi</code> from it.

#

91 older entries...

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!