Saturday, 7 June 2008

Folding in VIM

Code Folding in VIM

Vim is arguably one of the most awesome editors around. I use it exclusively, extending it with many plugins. As of version 6.0 Vim supports code folding, a nice way to hide blocks you don't need to see and thus reduce the visible complexity of the code you're editing.

Vim accomplishes this using a foldmethod which can guess where to fold the code based on syntactic hints (defined in the syntax file of the FileType you're using), indentation and other methods, including manual mark-up of foldable regions. It then calls a function stored in foldtext that computes a text to display instead of the text region folded away. You can see vim's default foldtext in action here:

Here used to be a screenshot showing some code blocks in VIM, but omploader.org think it's funny to molest people into not using their service, rather than just telling them...
I'll try to replace it soon.
Here used to be a screenshot showing VIM's default code folding routine in action, but omploader.org think it's funny to molest people into not using their service, rather than just telling them...
I'll try to replace it soon.

Rolling your own

I find Vim's default foldtext a bit hideous. Most importantly it messes up indentation and I'm very religious abuot indentation, given I'm a horizontal reader and use 8 spaces (one traditional tab) for indentation. So I tried to came up with something minimalistic that would preserve indentation on collapsed folds, using my traditional fold method syntax. I failed, but Heptite I met in #vim on irc.freenode.net helped me out with this code:

set foldtext=MyFoldText()   " Folds preserving indentation and without clutter function! MyFoldText() let i = indent(v:foldstart) return repeat(' ', i) . substitute(getline(v:foldstart), '^\s\+', '', '') endfunction   " Reload folding nmap <F3> :syn sync fromstart

The function MyFoldText() was his idea of approaching the problem. I had some trouble coming up with one myself because getline(v:foldstart) seems to include a leading indent which messed up the folding when a syntax element triggering folding (as defined in the syntax file) would be split across more than one line (like a bloated method declaration featuring many throws or arguments). I really think that this is a bug in Vim's folding method and eliminating leading spaces with a substitute command is just a workaround, but it works quite well for now and gives very pleasing results:

Here used to be a screenshot showing my current code folding routine in action, but omploader.org think it's funny to molest people into not using their service, rather than just telling them...
I'll try to replace it soon.

A look at a Tagger with code folding enabled.

Friday, 6 June 2008

Shit happens.

... only to you and when you'd need to get stuff done as quickly as possible
Here used to be a screenshot showing one of IBM's documentation resources about UIMA down, but omploader.org think it's funny to molest people into not using their service, rather than just telling them...
Ah well, I'll need to remember that docs myself now. That'll teach me to sync documentation offline. BTW. Another great example why creating a dark-net to store something as harmless as documentation and/or howtos is braindead. If they hadn't forced me to sign up for their service, I could've used The Internet Archive or the Google cache.

Sunday, 25 May 2008

Uncanny Rattles at Night Time: Johansson Sings Waits

So I decided to write something up about this new album I recently downloaded after hearing about it from people at kaioo.

I've been a huge Waits fan ever since I listened to Frank's Wild Years for the first time. His exotic grumpiness, his vocals, lax and stern at the same time have fascinated me from the beginning. The music is alsways a bit chaotic, most of the time the instruments provide only a thin layer, every one is just covering its own spot, its own frequency, while Waits' voice paints the songs in melancholy and wisdom. For me, Waits is like a father I never had, he feels like someone able to teach you about the miserable aspects of life, about pubs, prostitutes and alcohol. And music.

I've never really heard much about Johansson before; I knew she was some kind of Hollywood celebrity, but I never really was into Hollywood nor celebrities. Only after investigating a bit I realized I had seen a movie starring her: Lost in Translation. I didn't really like the flick though, it felt mediocre.

So, a mediocre (by my standards) actress starts singing Waits songs? This could either be a complete catastrophy or really interesting. Anyways I had to give the album a try, so I went out to get it. But I was afraid to touch it for another two days.

I had expected a bubblegum pop album. Or a really really good album. Or something entirely mediocre and trivial. It was neither.

The album still feels entirely strange to me. I can't really open my mind. Do I like it? I don't know. I don't hate it. But I expected something different and hence I'm completely disoriented. I must say that I can see Lost in Translation in this album. But, actually, the good parts of it. The waiting, the to-no-avail-ity, a lot of its melancholy. It starts completey soundtrack-esque, with an instrumental intro. The first bits of her voice in Town with No Cheer, are preceded by caribbean-sounding steel drums - and manage to surprise you. I expected her to sing better. After all, she had all the money of the world to let all sorts of machines sing instead of her. But, as someone posted in her shoutbox at last.fm - They didn't even try to pretend she can sing. They just let her go away with slight mistakes in pitch and a pretty monotonous voice. A voice not without charisma, but also with a lack of talent. She'll have to make up on it with character and innovations in future releases. I'll let it slip for this one, but I won't for the next.

All songs are covers, except for #6, Song for Jo, my favorite. This tune and Green Grass, #7, are the greatest songs on this album.

The atmosphere is very post-y, very melancholic. In contrast to Waits' instrumentation, the album feels a bit over-produced, too much reverb, to many stray notes wandering in a sound scape that feels very impressionistic overall - whereas Waits feels more like an expressionist. Instead of the contured, sharp tone of Waits, Johansson and Sitek deliver blurred and fuzzy sounds, sometimes even drone like. Her voice feels like Karaoke on some tracks, especially on I don't wanna grow up, a ... Disco tune! She appears to be a girl next door, taking huge sips from the 90's trash cocktail, while casually mentioning that she'll "drink you under the table" in I Wish I was in New Orleans, the only of Waits' earlier tracks on here. Unfortunately, she (or someone else) decided to pick mostly Waits' later works, which I'm not that familiar with.

Overall, this album feels strange and new. It's like soundtrack meets Waits meets that cute lost little girl in Japan, that's actually Danish, but an American actress. I tagged it drone-pop and post-waits since that's what it feels like for me.

I think I like it.
It would be hilarious if Johansson started a nice alternative/experimental music career frome here. But somehow I think she won't.

Saturday, 24 May 2008

Use the Cluebat, Luke!

Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal.
F. Nietzsche

Sometimes us programmers are faced with what I would call the most ridiculous and humiliating aspect of our work:

You come across a problem. You start thinking about it. You find it ever more complicated, but you go on and on. Actually, this is only one of the obstactles in your way to achieving your original goal, it's probably not even got something to do with what you were trying to accomplish in the first place...

Finally you come up with a solution. It took you 2000 lines of code, it reduced your app's performance by 40%, true, it still may have some bugs you don't know of - along with a couple of bugs you know but have no idea how to solve and hence try to sell as features; but, hey! the problem was complicated, after all! And you solved it. Yay!

The glistening, glaring enlightenment strikes you once you're presented with a comment such as

"Why didn't you just use random one-liner-solution X?"
on your work.

Of course, your immidiate response goes "Huh?". Your brain feels like it's shrinking. No words available, they're all gone, along with your self-esteem, your thinking your're intelligent and all your coding skills. You've just spent a week's worth of coding time in vain. Everything you did was ugly and totally uncalled for. You take the blame.

This happened to me yesterday, and the apathy and fear of code is still here, hampering my productivity and just about everything. If it wasn't for the great music pumping out of my speakers, I would even not feel competent enough to blog about it... Here's what I did.

I'm currently working on porting a second-language-acqusition-aiding-system (Think taaaanks! We need a nice buzz-word for this, pronto!) from mod_python to Java & Servlets (in Tomcat), utilizing the Apache/IBM UIMA-architecture. The program fetches remote web sites and performs (not-so-)random input enhancement on them. The problem was displaying remote websites properly on my own server, just like Google Translation does. Long story short, I went to considerable lengths to replace all relative paths in a given page with absolute ones to get the source adresses for extra content right. It worked. Sort of.

Then I went to my advisor, and, whoops... Ever heard of the HTML <base> tag? Me neither. It's not even ONE line, replacing all of my crafty and barely readable regular expressions. My conscience faded. Good thing I didn't drop dead right on spot, though I surely wanted to.

Now I have to gather focus on performing the serious tasks at hand. Fortunately that's something I should know better - really, I think this sort of mistakes happens because you hit a spot in your app where you have to perform a task you don't know shit about. You don't know the libs people use there, you don't even know how to approach the problem. I asked people on #tomcat and #java on freenode.net, but they couldn't help me either. They were all programmers, I should've asked some web designer. But I didn't... I went all finite state on it, bombarding the poor thing with hideous regexps, wasting my time on a problem that didn't really have to exist in the first place.

Oh well, 'tis one of the moments, where I just tend to pessimism, reminding myself about this great quote I came along in Nietzsche's "Menschliches, allzu Menschliches". Strangley, pessimism, like doom metal makes me go optimistic.

Nothing lives which would be worthy
of your striving, and the earth deserves not a sigh.
Pain and boredom is our being and the world is excrement,
—nothing else.
Calm yourself.
G. Leopardi

There it goes. People screw up stuff all over the place. This time it had to be you. You're not even special in failing. Go on, dipshit.

Saturday, 26 April 2008

Where Sun Don't Shine

Sun are a big company. Really big. They created some of the most reliable server workstations, some of the fastest processors, some of the most stable OSs in the world. And they created the as of now most-used programming language, Java. Java is sort of nice. It's dumbed-down C++ with some enhancements, it's not feature rich, but nobody in their right minds wants a programming language to be feature rich. I'm a big advocate of "the right tool for the right job" and feature rich languages seem to contradict to that credo. Java is the right tool for many jobs: GUI creation in Swing is easy and straight forward, networking closs platform is painless and portability is one of Java's main selling points - because it's really easy. The only thing you have to keep track of is the JVM and source version and there you go.

I guess if we lived in milk-and-honey-land, that would make Java a perfect programming language. But it isn't, and milk-and-honey-land is far off. Java was supposed to bring interactive web pages to the clients more than ten years ago (note that JavaScript has nothing to do with Java). It failed. And miserably so, I must say. There's nothing that turns me off more than a page using a Java applet. They are ugly, they are slow to load, memory hogs, and they are buggy.

But I don't care about applets. Servlets are a much better technology anyways, and I found Java to be really useful for Web programming. But this rant is really about Java on the Desktop and about Java for the programmer. Over the years, I've come about some rather annoying misfeatures, bugs and Sun's stubborn way of not wanting to address bugs that have been known - literally - for years.

  • Swing sucks, after all. It's ugly. It's slow. It's bloated. It lacks a set of really useful and easy-to-customize stuff in the library. Heck I remember hacking Delphi used to be more fun than Swing. At least you didn't have to call.thatMethod().somewhere().InAnInnerClass(WayDown.theCamelCase.objectHierarchy()); But see below
  • Swing sucks even more. Ever tried to run a Swing app on X using a window manager that is not one of {Compiz,Metacity,Kwin}? Well, good luck. <3 grey boxes. This has been a bug for a long time, Sun claim it's fixed. It ain't. It's fixed for Compiz, everything else still has problems. This is because some mind-bogglingly stupid intern at Sun had to hardcode the goddamn fucking names of Window Managers into .equals() checks of the kluges used for circumventing the original bug. The result? My favorite window manager, XMonad, displays Protege like this.
  • Many people seem to argue against 8-space indentation on the basis of Java code exhausting horizontal screen space even on 1900xX resolutions. That shouldn't be the case. typedefs, operator overloading, sane object names (SomethingBadHappenedButIReallyDontKnowWhatExactlySoHeresAnException, anyone?) and more modern stuff like partial hiding/importing/exporting (like in Haskell) could greatly improve on the situation.
  • Inexplicable syntactic difference between dynamic and static arrays. Yeah, I know, it's just syntactic sugar. Go write your stuff in ASM, then, if you don't like that.
  • No way to generalize a static array. If T is a generic type, T[] stuff = new T[x]; is illegal. But a function header like void some_function(T[] stuff) isn't. This means that you actually can have static arrays of generalized types, but you have to work your way around like T[] stuff = (T[])Object[x](); This is ugly, and, moreover, it's dangerous. It's also a restriction that hits the language quite hard. Don't believe me? Read on...
  • Not only that, but you are not allowed to create arrays of inner classes of parameterized classes. That's right. No, I know it's stupid, but it's the way it is.
    public class General<T extends Something> { InnerClass[] ic = new InnerClass[10]; private class InnerClass { int data; } }
    This will not compile. It fails with the oh-so-descriptive-and-helpful error message "generic array creation". There you go, the inner class doesn't even use that freaking parameter type. It's enough for it to be a subclass of a paramaeterized type. This is especially annoying when you have to create, say a tree class that is to store all sorts of interesting different types. That would be what them Generics are good for in the first place, right? Well, if you want the tree to have, say, 4 children per node, you'd love to do that using a static array, right? And inner classes are usually the best approach for making a specialized Node class... But in Java, you have to suffer compiler warnings and unsafe typecasts to do that.
  • You can't pass functions as arguments. That's because there are no function pointers. Meh. That makes NLP systems and natural language processing (especially computational semantics) unreasonably complex, because you'll have wrap it all up into classes. But this may fall under the right tool for the right job clause. I've long since wanted to switch to Prolog, C++ and, lately, Haskell for natural language processing. They're way batter at that than Java.
  • This isn't even as much as I expected it to be, but there's more that I've missed for now. Jamie Zawinskie has a lot more on this, but I do not share all his views. Maybe it's that I'm just no C programmer and therefore I have a whole different set of expectations. Maybe I just don't understand all of his points, but I couldn't agree more with most of the stuff he describes. When you google for "why java sucks", you will find a whole bunch of pages. About 3000, for the phrase query. That's a lot for a phrase query.

    I don't say Java sucks in general; it's an OK language for quite a number of tasks. And it's probably even the language I know best. But it has deficiencies, and quite some and then some more. I'll be moving on to greener pastures from now. Prolog sounds nice, it's well suited for what I'm currently doing. Haskell sounds even nicer, and I spend every free minute I can find with monads and similar stuff. I'll probably blog about it, too, when I'm good enough at it. ;-)