2010-04-26

Languages, OOP, Sather et al.

I used to dislike OO programming paradigm. I suppose the main reasons were that I started programming with imperative languages and loved so much mc68k assembly, and that people trying to convince me about the advantages of OOP failed —often talking about the wrong feature in the wrong way (e.g. magnifying functions overloading... which is not a OOP-only thing, e.g. Fortran 95 and later has it!)

Moreover, because of reasons I don't know, C++ does not light my enthusiasm. And so it is for Java (recently I've deleted Google Android SDK... tried to get interest into J2ME for a brand new phone I now own, but it is always... well... Java). I changed a little bit my mind about OOP when I first came in contact with Objective-C. I feel it is the right language that exhibits the right amount of OO features; OO concepts are insinuated in the "C core" better than C++, and Objective-C can at least one thing that currently C++ (AFAIK) can't: extending classes we have not the code of.

Objective-C takes something from Smalltalk, and this language too can extend a class without touching its code. I like Smalltalk too, and luckly GNU Smalltalk can be used as an interpreter, allowing file-based programming. Smalltalk is more "coherent", I mean: everything is really an object. It is not so for Objective-C and C++, which inherited (!) from C primitive types that are not objects. However C is still my preferrend imperative language (when I don't want to stick to Fortran and interpreted languages like Perl are inappropriate), so Objective-C win against Smalltalk, that, by the way, is also not so efficient (read: it is slow).

Got over the primitive "hate" for OOP and OO languages thanks to Objective-C and Smalltalk, there are other details that can make me dislike a programming language. E.g. I don't like that "white spaces" have syntactical meaning. Thus I could like Python (and Haskell), but indeed I ended to dislike them. Another detail is when upper-case or lower-case letters have syntactical meaning too. This detail would rule out other languages (even though I admit they can be cool and powerful).

But recently I decided to take a look at Sather. I wanted just to taste it. Sather uses all upper-case letters for classes, but other OO peculiarities are again more elegant than how C++ or Java take it. The syntax looks to me clear, I like how iterations are performed (I've seen something similar in Ruby), and ... we can create superclasses.

Creating a superclass could mean we can add "behaviours" to a "middle" class. Let us suppose we want the INT class respond to a factorial method (if not already defined into INT). We can create a superclass of INT holding the method. When it is called, the INT class, having no that method, "passes" it to the superclass... where our new method is defined. The result is that we can write

4.factorial


to obtain the factorial of 4, where 4 is an instance of INT which alone has no factorial method. It sounds good, but it does not work. I've tried to understand what's going wrong, but failed. Sather language documentation is spartan, lacks details (to know builtin methods of standard library I had to take a look at the code!). The tutorial uses superclassing, subclassing and abstract classes for typebounds (in generics), even though a sentece let me hope it is possible to use superclasses to obtain what I want. Likely I've not understood something important, but surely "extending" a class is not so natural and easy as it is in Objective-C
and Smalltalk.

I think I won't go too much far with Sather, but first I would like to try to write something really "OO" in Sather. I feel that, from the OOP point of view, Sather has some oddities; I would like to focus them to compare it better with other OO "solutions" (i.e. languages), in other words to confirm that the worst implementations of the OO concepts are those of the most used and widespread languages.

2010-03-30

One-liners and why to get it harder than it could be

Since I've updated kernel and X server, my computer became slower... new "softwares" require new hardware to do the same things. This is a fact that always upset me. Anyway, I abandoned KDE (unusable... and half-mixed KDE3-4!) and installed WindowMaker. I also abandoned full graphics file-manager (avoid the loading of Qt libs and KDE services, being used to use Konqueror, uncomparable to Dolphin), tried lightweight Gtk file-managers but none satisfied me. I used midnight commander for a while and tried also gnu-git ... But now mainly I use simply the command line, in a simple xterm of course...

So it happens that something easy and fast with a filemanager becomes a little bit harder. But still the command line, with all GNU tools around and interpreters of languages like perl, is powerful.

When I download pictures from the cam, I need renaming them according to EXIF date; so I've created a script (exifrename.sh), which uses exif the extract the date and rename the object the way I want. I use it like this

exifrename.sh *.jpg |bash


The pipe is because the script outputs the commands rather than executing them (it could be useful to check everything's fine).

My cam creates a directory for each day, like NUMcanon where NUM is a number not related to the day. What I want is to copy just some days, not all, into the local hard disk and in the same time I want to strip the "canon" part. It would be easier maybe to copy the dirs and then rename them. But less fun for me. So what I do is

for el in 101 102 105 ; do mkdir $el ; 
pushd $el ;
cp -r /media/usbpen/dcim/${el}canon/* . ;
popd ; done


Ok not so funny but useful to... Recently I had to convert several scattered .doc (alas Microsoft stuff) into HTML, I've used this

find . -iname "*.doc" |
(
while read line;
do wvHtml "$line" "${line%.doc}.html ; done
)


As last example, a "oneliner" to read FASTA headers / sequences and sort them (just the header). Don't ask why, I've found the request on Yahoo Answer.

perl -e '%p=();
while(<>) {
if (/^>/) {
s/^>//; chomp;
foreach $v (split("\x01", $_)) {
$p{$v} = 1 if !exists $p{$v} ;
}
}
}
print join(" ", sort keys %p)' <fasta.txt


A perl guru can make surely better (and what about perl6?), anyway the solution worked .

Now we discover that there are things that can't be done simply by a graphics interface but... These examples are taken directly from the history of the shell; there are other two similar examples. The history has 1045 entries. Of these, 1039 are mainly simple commands like cd, rm, cp, ls, mkdir (and sometimes mount and umount). Actions that would have been a lot easier using a graphical file manager!

So the problem said at the beginning is back: I need keeping running few processes/programs. ... I'm going to hate the computer business, because I can't see a reason why to do the same thing I did before now I need more memory or a more powerful processor... or downgrading the software (meaning I should compile by myself a lot of codes since oldest packaged pre-compiled programs are already too recent, or there are too many dependencies issues I don't want to cope with!)

I hope things will be better, but looking around I see things indeed get worse: we can say Berlusconi is the winner, and this means there's no hope to change for Italy (it seems OT, but is not...).



Ah, the exifrename.sh code:
for el in "$@"; do
pathpart=$( dirname "$el" )
namepart=$( basename "$el" )
data=$( exif "$el" |egrep "Date and Time \(orig" \
|sed -e 's/^.*|\([0-9]\{4\}\):\([0-9][0-9]\):\([0-9][0-9]\) \([0-9][0-9]\):\([0-9][0-9]\):.*$/\1\2\3-\4\5/' )
echo "mv \"$el\" \"$pathpart/$data-$namepart\""
done

2010-03-26

Just to Begin

What should I write just to introduce myself or rather this blog? I really don't know, thus I won't try to be smart, cool or whatever. Basically I've nothing to say, so I can say everything, though in an English that could make someone shudder. And I'll begin from the very end. Few days ago (maybe already a week or more!) I was back from hospital. Recently I underwent a surgical operation, something like this (indeed, precisely it). Has this fact changed my view of life and universe? Nothing at all. Has this fact brought to me a new awareness about something? Maybe yes. Did I see a light, a bright tunnel, a whirl and hear a voice telling me my time is not come yet? No. Simply turned off for a while. No vision, no memory, no thought, no pain. Satori? I don't know but it was not unpleasant, and this is the outstanding fact. Unpleasant were the painful awakenings (two).

Will I talk about this in the blog? Not at all, luckly! It was just a way to begin and explain why I haven't been online for several days, a month and an half, more or less. Since January, 30, until a day in March. So you're wondering what this blog will be about... I think, but I am not sure, I will put in it words people can disregard, maybe opinions, maybe witticisms, maybe codes, ... ideas coming into my mind, without necessarily a logic tie, nor the claim to be interesting for a supposed audience. And of course without the promise for a perfect English... The only language I can write in correctly is Italian. Why doesn't I write in italian? Because I prefer to imagine a supposed larger audience.

That's all for this first post...