Thursday, February 28, 2008

Functional Programming in Java? No, not Scala...

Having more experience in Java than most of the other languages I've coded in, I was interested to read this article from 2004 on using some functional programming concepts in vanilla Java.
The notion is attractive, but then I saw some example code:
public int calcPriceInternalIterator(List items)
{
Iterator i = items.iterator();
UnaryFunction getItemPrice =
new UnaryFunction()
{
public Object evaluate (Object obj)
{
return new Double(((SETLItem)obj).getPrice());
}
};
Constant usd100 = new Constant(new Double(100));
BinaryPredicateUnaryPredicate moreThanUSD100 = new BinaryPredicateUnaryPredicate
(new UnaryCompositeBinaryPredicate(new IsGreaterThanOrEqual(), getItemPrice, usd100));
FilteredIterator fi = new FilteredIterator(i, moreThanUSD100);
Summer addPrice = new Summer();
Algorithms.foreach(fi, addPrice);
return addPrice.getSum();
}


And then in the concluding spiel:
In this article, I've introduced two functional programming techniques that can be easily integrated into your Java development practices.


... Now come on. I have no idea what that code does, because it's an ugly, bloated mess of syntax and verbose names (not to mention simply having far too many identifiers and tokens in the first place).

Java is a round hole. Functional programming is a square peg. Without modifying the syntax of the language, trying to fit the square peg in the hole may be possible, but far, far too complicated and impractical. No sane coder would want to work with that.

Tuesday, February 26, 2008

week of doom

Cycling back from snooker on Tuesday, I stopped in a Chinese takeaway in Beaumont, about 3km from home. Laden with chow mein and... whatever I got for my girlfriend, I decided to cycle on the path to the petrol station just around the bend (where I could buy a 2L bottle of something to drink, since coke cans are €1 a pop in the takeaway), rather than bother to cross back onto the road, cycle for 20 seconds and cross again to get to the petrol station.

I slightly underestimated the distance to the petrol station, so when I got my bottle of Club Orange and stuffed it into my backpack beside the Chinese food, I sat on my bike and noticed the front tyre was completely flat. Bollox, said I.
Anyway, I didn't want to faff about trying to fix it there because that takes me an hour and I had hot food in my bag. So after a longer-than-usual uncomfortable cycle home on a flat tyre, I pulled the takeaway out to find that it had leaked all the sauce from my chow mein into my backpack, all over my rain trousers and whatnot. And bollox again, I swore.

Next morning, we spent an hour fixing the puncture (I hate the damn things. Hate.) and I headed off to DCU. Some hours later, when I returned to my bike I noticed it was flat again. No problem, probably a slow puncture elsewhere, or a semi-sticky patch.. I'll just pump it up again and ride home. However, when I took the pump to college in my bag, I neglected to take the small attachment tube which connects it to the tyre's air valve. For the third time, bollox was uttered with some frustration.

Another trip home on a flat tyre, listening to the rim grind against tarmac through the outer tyre, and I left the bike 'til morning before swapping out the cursed inner tube with another I'd pulled out months ago with only one prior patch and which seemed to need only one more. This successfully carried me to college and back, but two days later it was flat again.
I put it out of my mind and on Sunday night, watched Lost at 9pm and prepared to do some catchup piano practice at 10 and a little more on Monday morning... when at about 3 minutes to ten, the power failed... apparently only on my road, or my half of the road even. So that lasted until about 3pm the next day, which caused some trouble since the piano is an electric keyboard.

When life gives you lemons, make cake! Unless you have a poxy electric cooker and the power is out.

...On the plus side, when I opened up my Macbook which had died in its sleep during the power outage (I put it to sleep with about 20 minutes left on the battery monitor, soon after the cut), I expected it to go through a normal bootup. Instead, I saw a funny progress bar whizz for a while, before it brought up the usual unlock dialogue which appears when you're logged on and wake the machine up. So there's a point where it wakes the machine from S3 (suspend-to-RAM) sleep and puts it straight into S4 (suspend-to-disk), then powers off, on the basis that the battery is about to run out which will destroy your S3 image and you'd have to boot again. Nice.

Sunday, February 17, 2008

The joys of Sozobon C

Having been slowly inching towards doing a little coding on the Atari ST, I decided to set some (very) small, straightforward goals to actually get started doing something useful. This seems to be a pretty important tactic for me, as otherwise, I can start to get lost in the haze of all my different interests and never really get anywhere.

I started laying these out in a simple todo list following the extreme programming task card style we used at Cellusys, like so:


 In progress:

 todo:


* ST: Display a sprite! (IH: 2)

done:

* ST: Switch to low res and back to original. (IH: 0.5 / AH: 0.5)
* ST: Print the current colour palette. (IH: 10m / AH: 20m)
* ST: Change the background colour to green. (IH: 2 (??) / AH: 10m)

Printing the palette took a bit longer since I decided to write a number->binary (string representation) conversion function, and realised I'd forgotten some pretty basic things about C. But that's part of the fun! Note my fairly pessimistic estimate of 2 hours to display a simple sprite. It's probably a good idea to err on the side of underestimating yourself when planning stuff that is somewhat beyond your ken, even if you used to do it ten years ago. To be honest though, I'm not really sure what to try after that. A little animation I suppose? Then eventually some screen flipping and fiddling with the palette and an XBIOS sound routine. Sounds like a plan...

Wednesday, February 06, 2008

The 1 percent rule

From a Wired article on geekiness in Futurama:

Cohen refers to a One Percent Rule that guided the writing on Futurama. When they were scripting the episode "Kif Gets Knocked Up a Notch," a character claims that he painstakingly programmed a holographic simulation using "4 million lines of BASIC." One writer pointed out that 99 percent of the audience wouldn't get the reference to an old programming language. Producer-writer Eric Kaplan responded, "Fuck them!"

Cooool.

Friday, January 25, 2008

Lambda the Ultimate! Mayest thou wrap with great haste.

While reading this interesting thread about "concept [representation?] programming", I immediately became frustrated at the massively long lines which forced me to scroll left to right and back constantly.

I started to search the web for a fix, tried using Google Translate (Chinese to English) to see if it would somehow fix the problem and when this failed, took a quick look in Opera's "view" menu. There I saw the obvious solution, "Fit to width". I kind of expected this to zoom the whole page out so small that I wouldn't be able to read anything, but nope; it just forces the page layout to conform to your browser window's width, effectively forcing word-wrapping. I love Opera.

Saturday, January 12, 2008

Dude, where's my fonts?

After a bad crash on my MacBook, I rebooted via the power button and logged in again to find my fonts banjaxed (i.e. most of them missing, notably Chinese character fonts). A cursory investigation led me to check the Font Book application, which should contain two libraries called User and Computer, which contain the default set of fonts.
In my case, only the All Fonts and Gaeilge libraries were left, both empty. Trying to add more fonts had no effect, until I eventually realised I had to create a new library as a workaround and add fonts to that.
This worked, although due to the aforementioned .CFUserTextEncoding problem I had to open Font Book every login before applications that used interesting fonts, or they wouldn't find any.

Just for the sake of it, I opened up a bug report with Apple, not really expecting to hear anything back. A few weeks later though, I did hear back from them, with a solution that worked:

"Look for a directory called: com.apple.ATS inside of /var/folders

Once you find it, copy this directory to another location and then remove it."

Doing this and rebooting the MacBook, I logged in again and started Font Book to find, lo and behold (drumroll and angel choir) that the User and Computer font libraries were automatically added again and all the fonts loaded back into them. BRILLIANT, thanks Apple, I totally didn't expect that. Thumbs up from me!

Friday, January 11, 2008

The middle finger of .CFUserTextEncoding

Recently, I had a bad crash on my MacBook while it was downloading some torrents. When I came back, I had to use the power button to restart the machine, and after the reboot, some things were broken. First, Font Book contained no fonts. Second, LyX didn't run. Third, Atlantis (a MUD client) didn't run either.

The LyX error looked like this:
LyX: Creating directory
Same as entered returned
Failed to create directory. Exiting.


And Atlantis didn't give me any error message. So, after a long time fiddling around with directory permissions, changing my group from admin to user and chowning a bit, I noticed from a dtruss log of failed and successful (by simply running as another user with sudo) runs of the affected programs that the file ~/.CFUserTextEncoding was being accessed at some point.
So I had a quick look at this file in the failure (first) and success (...second) cases:
Macintosh:~ oisin$ more .CFUserTextEncoding
39:50
Macintosh:~ oisin$ more ../yunqing/.CFUserTextEncoding
../yunqing/.CFUserTextEncoding: Permission denied
Macintosh:~ oisin$ sudo more ../yunqing/.CFUserTextEncoding
0:0

Replacing mine with 0:0 and su'ing to myself to get a new shell, I was able to run LyX and Atlantis with no problems. So, I'm not sure what this means - maybe the crash wiped out an encoding method or wrote the wrong data to that file, and when applications read from .CFUserTextEncoding they tried to look for/save their settings in the wrong place.

Confusing, but at least there's a fix.

Monday, December 24, 2007

Where did Installer go in Leopard?

My Macbook crashed hard for the first time last night, and a few things got bolloxed up. For one, my fonts were and are still knackered - opening the Font Book application shows 0 fonts, even if I tell it to add fonts. Adding fonts it tells me not to add causes it to use more and more memory until the allocator won't give it anymore and it dies (around 1.6 gigs at this stage).

Another problem is that file associations got nuked. Fair enough, I can rebuild those as I run into them... but when I tried to apply a patch to NeoOffice, I'm told:

There is no default application specified to open the document "NeoOffice-blah.pkg".


Well, that's a bit strange, so I google and find that I need to use /Applications/Utilities/Installer.app for these. Great! Or not great, because the file is MISSING. I see similar problems on forums (what would we do without them? well, without the ones with actual answers anyway), and people generally recommend the poor user to reinstall via "Archive and Install" which supposedly keeps your user files/settings.

Before I made such a serious commitment to dig myself deeper in the shite, though, I did another quick google and found that as of OS X version 10.5 "Leopard", the Installer application is now located in /System/Library/CoreServices, along with a horde of other utilities.

So to others encountering this problem, DO NOT REINSTALL before checking there. It's unlikely that the program has let itself out the back door into the night. It's probably right there, hiding under your bed.
Don't be too hasty to reinstall your OS - there's usually a reasonable fix out there... Now if I can only find my font fix...

Tuesday, November 13, 2007

C++ FQA

This very good read takes the form of responses to answers on the C++ FAQ Lite. The author makes no attempt to hide his contempt for the language, and for well-explained reasons (for example, that the C++ grammar is actually undecidable and thus parsing it can sometimes have bizarre results).

I'd recommend this to anyone caught in the popular grip of C++, rather than finding out (or being caught in denial) about the pain awaiting programmers of any level. Hint: lack of garbage collection, extreme, multi-pronged lameness of templates and not-exactly-encapsulation feature heavily.

Personally, I've been working in Java for the last while anyway, but I've promised myself I'd get into (Smart)Eiffel for a while now, which seems like a much, much simpler and unambiguous language with garbage collection, a decent string class and no poxy operator overloading, and still reasonably efficient in the general case, compared to C++. Also, the D programming language may be worth a look, which seems to have some good built-in string handling (but no multiple-inheritance. Meh?).

Friday, November 09, 2007

Backflip

Everytime I try a backward somersault or handspring, I chicken out as soon as I start to fall back... like so:

How are you supposed to override that annoying reaction?

Thursday, November 08, 2007

Mac an Leabhair... the Macbook

Another fairly large skip in blogging activity... so, as part of my scholarship funding, IRCSET allow first year research students to buy a computer and put the tab on their expenses account, to the tune of €1500. Naturally, I arranged things so the computer and components came to €1490 or so - almost a bin-packing problem, but easily approximately solved.

The Macbook, a 13" white one (why pay 200 quid just to have it in black? pfft.... that'd only absorb heat and burn your err... legs, anyway) with a 2.16ghz Core2 Duo, 2 gigs of RAM (the most I've ever had in a machine before was a sad 512 megs) and a 160 gig hard disk (for research... and anime and music... not porn. Fine, for porn - are you happy now?).

Unfortunately, it came with two problems:

  1. The lock the sales guy sold me over the phone, which he assured would fit my machine, didn't fit the machine. In fact, it damaged the side of the case while I tried to gently fit it to the Macbook's lock slot.
    This is kind of an understandable mistake - it looked almost exactly the same as another lock that did fit, and I didn't see anything on the Apple website about which lock fits which machine.
  2. The version of OS X, which the salesman assured me would be 10.5 "Leopard" when I specifically asked him (and the previous salesman on the phone), turned out to be 10.4 "Tiger". Not that Tiger was bad, I was still delighted with its performance compared to my Windows XP, Ubuntu XFCE/Gnome and Mandriva KDE experiences. It was ridiculously fast, smooth and yes, things "just worked", to appropriate a shitty phrase.

So, for a couple of days I tried to get in touch with the sales guy, who seemed to suddenly go on annual leave, then I eventually got on the phone with another agent in Apple Education, who, when I called her for the second time 5 minutes later, told me an install disc for Leopard was on its way and I could keep the incompatible lock for which they had already sent out a compatible replacement, both without charge.

That just leaves the slight damage on the case from the weird lock, but I'm pretty impressed by their helpfulness, especially compared to Acer, who I dealt with when one of their firmware updates killed my old laptop's flash BIOS (those useless assholes charged me over €300 to replace the entire motherboard, because they apparently have no mechanism to recover from flashing the BIOS with an image from an 'incompatible' revision of the same motherboard model. The Taiwan guys sent me a text file describing some Phoenix BIOS "bootblok" recovery procedure, but either that wasn't implemented or the Official-Acer-Repair-Shop-Off-The-Naas-Road-Dublin-12 engineers were ripping me off).

Apple.getInstance().respect++;

Wednesday, October 10, 2007

grub got your goat?

GParted decided to balls up before finishing a move and resize job where I wanted to decrease my oversized swap partition from about 4 gigs down to 1.2 gigs and increase the size of the partition I used for storing big media files (immediately next on the disk).

So I was left with a slightly knacked partition table and decided to burn a proper GParted livecd which also contained a program called Testdisk. This was able to rebuild the partition table (although it disturbingly seemed to think the drive had 16 heads every time I loaded it, and complained that either this setting was wrong or the disk layout was crazy until I changed it to 255... every time), but at some point I managed to overwrite the MBR as well by picking the wrong option.

Attempts to reconstruct the GRUB bootloader's MBR claimed success, but actually trying to boot from the hard disk resulted in just a cursor flashing indefinitely. No message from GRUB, even.

Eventually, I found that the instructions I'd been following didn't tell me enough and that making the bootloader was a bit more involved than just:
"root (hd0, 1)
setup (hd0)",
and certainly more difficult than just running "grub-install /dev/hda2", which wasn't at all happy.

In the end, I started copying an entry from my boot partition's old GRUB menu.lst and replacing the now-inaccurate drive references:

The final sequence of commands to fix the thing was something like this:

root@gparted:~# grub
[ Minimal BASH-like line editing is supported. For
the first word, TAB lists possible command
completions. Anywhere else TAB lists the possible
completions of a device/filename. ]

grub> root (hd0,1)
grub> kernel /boot/vmlinuz-2.6.20-16-386 root=/dev/hda2 ro quiet splash
grub> initrd /boot/initrd.img-2.6.20-16-386
GRUB Error 16: Inconsistent filesystem structure
grub> setup (hd0)
[snip a few lines of blah followed by "success"!]
grub> quit

root@gparted:~# reboot


The "error 16" line when I tried to set up the initrd threw me, but after running e2fsck on the partition at least twice I decided it was just GRUB being a bollox and went ahead with it. GRUB's own filesystem mini-tests suggested things were okay anyway. So after rebooting everything was ok.... although once I logged in, I ran a command called "update-grub" which may be Ubuntu-specific. This seems to have incorrectly specified the root partition, but we'll see...

So anyway, try that more verbose tactic before executing GRUB's setup command, if you run into a similar problem.

Thursday, October 04, 2007

swiping a computer

After a few days of limbo, having started my postgraduate course in DCU, I still wasn't able to get into the secure(ish) research bay. Nagging to the school office did no good, so I went to the Estates office in person against the recommendation of the school secretary. Handily, the lass in the Estates office swiped my card, noted that I hadn't had privileges to open those doors added and dragged the equivalent permissions onto my set - all of 2 minutes' work.
Then I went back to my desk, without having to wait for someone to let me in like an orphan boy, and waiting around for someone to set my nicked (from someone who hopefully wasn't using it anymore) crappy Pentium III PC to allow network logins.

I'm going to assume that this amount of hassle, bureaucracy and poor inter-department communications is a normal thing.

Saturday, September 08, 2007

fantasy... reality. fantasy... reality.

LOS ANGELES (Reuters) - Vanessa Hudgens, the star of the wholesome, made-for-kids TV movie hit "High School Musical," apologized on Friday for a nude photo of her on the Web and Walt Disney Co. said it was sticking by the performer.
...
"I'm devastated because I have an 8-year-old for which I now have to have an explanation," said another Los Angeles-area mother, Rosie Konkel. "She's always looked at this character as a very smart and proper young lady."

Here we have a problem. That is, these people seem unable to distinguish between the character played by an actress, and the actress herself, who has her own, (almost) private life. For example, just because the character is an unrealistically good, innocent girl does not imply that the actress playing her should be the same. That's why it's called "acting".
This seems like a fundamental division - character and actor - which should be understood by even young children, yet some adults can fail to grasp it.
I heard that an actor who played a rapist in "Fair City", the crappy soap opera, was spat at on the streets and threatened for his 'crimes'. Which is bloody ridiculous - do these people think all TV is a fly-on-the-wall recording of real life? How could they possibly fail to separate the character from the actor?

Maybe you should be forced to take an IQ test before you turn on your TV.

Friday, August 10, 2007

/etc/init.d/me status --not-too-verbose

Amazing what finishing college and quickly walking into a full-time job does for your free time. In the sense that it almost totally erases it. But I'm alive, anyway. And the job is okay, writing a testing utility for ATM controller software. Whee.

Handily enough, one of the guys I made friends with at work introduced me to the Sportslink snooker club in Santry, so I've got a chance to practice some proper snooker and improve a little, rather than bang my head off the wall on my own.

At the moment in my fragments of spare time I'm sleeping, watching the X-Files and very slowly learning three languages (Eiffel, AutoIt v3 and Chinese). Verrrry slow....ly....

Tuesday, June 26, 2007

grid projectoring!

I was pretty impressed by this new product called Pluribus - presumably from the Latin motto that appears on American currency meaning "From one comes many" (probably also on a counterfeiter's t-shirt). So, it uses a webcam or something to dynamically adjust its configuration to split a single video stream over any number of projectors to make a bigger, better quality image. And apparently you can just plug projectors in and plonk them down anywhere and it'll do the stuff automatically.
Good idea! I'm sure it'll be approximated in open-source form to use your standard webcam, if Pluribus turns out to be expensive...
Here's a nice shot showing the boundaries of each individual projector's display...

Monday, June 04, 2007

the vacuum of "just-finished-college"

It's over. It's over. It's over. Takes a little while to really sink in, but now that I've finished my degree in Computer Applications (and what a truly fun degree it was!) at DCU, I'm free to do all those things I planned to get into during college but never had the time.
But I still haven't really done any of it, except for starting to learn Eiffel. Anyway, I'll have to get cracking before I find a job, or I might never start on it.

Just in case anyone is interested in a (not very)proof-of-concept program where you can train the TD(λ) players against either other temporal difference learners or against random players and view the plays made in a single game, I've put up the windows/linux binaries here.

Sunday, May 27, 2007

gcc as a cross-compiler

I've been following the steps outlined on Patrice Mandin's site to set up a cross-compiling version of gcc-3.3.3 that will produce outputs for the m68k-atari-mint target. I'm not running MiNT, but this seems to be the closest thing to building an Atari TOS compatible binary.
What I don't understand is the amount of work necessary to do so. I first have to compile an oldish binutils with a native x86 linux target, then start using that binutils to build the same version of binutils, this time for the 68k Atari target... then build the entire gcc-3.3.3 for x86, and then again for 68k. Jesus.

I thought the whole idea of a modular compiler design was that it wouldn't cost too much to implement a new target machine - just a separate backend that can be plugged into your existing compiler, that accepts intermediate representation code from the frontend and produces assembly specific to the target machine which it can assemble and link.

So why do I have to recompile binutils and the entire gcc twice each? This doesn't seem right.
But then, I don't know much at all about gcc - perhaps there are other, much easier ways of accomplishing the same goal.
Please comment if you know anything about adding new backends to gcc!

Saturday, May 12, 2007

Postscript looking manky?

Since I got my nice new cheapo network laser printer set up in Linux and Windows, I've noticed a problem when printing some Postscript documents from Linux. The pages will look fine on screen, but some fonts won't be sent to the printer (or something) and it will use a proportional font (e.g. Helvetica)'s character spacing with Courier instead, a fixed font, which results in totally uneven and sometimes overlapping characters horizontally. I read somewhere that Ghostscript has much more fonts than Postscript, so perhaps not all the font data is being sent to the printer and this is the problem.

I scratched my head for a while, trying different viewers and the like, but eventually stumbled, completely accidentally on a fix which works for me, although results in massive bloat:

Run ps2ps on the postscript file. This is supposed to 'optimise' the postscript file somehow, but so far has resulted in them increasing in size for me, and looking weird on screen... however, the printer output is absolutely spot on. It just seems to convert everything into image data... which is a lame fix, but suits me for printing.

I'd appreciate it if anyone else knows more about this and maybe a better solution!

Tuesday, May 08, 2007

more exams

Another set of exams coming up. This time, I think I'll fall back on the tactics I used in 2nd and 3rd year - just reading as much as possible, then using mnemonics to memorise lists of keywords to remind me of notable elements in each subject.
I'm not sure if I'll use Pauker at all for this - my exam performance in semester one this year was much worse than last year (68% average compared to 81%...??), probably because I was biting off too much to chew. The flashcards route needs more experimentation for general study.
Anyway, this time there are only three exams, one of which (Compiler Construction 2) is almost a gimme (although I thought its predecessor was a great exam and only ended up getting 63% or something on the paper, so I'll have to be wary), one of which should be alright given a bit of care and attention (Real-time Embedded) and one of which will require a bit more work since it's so bloody abstract (Z).
That said, JM seems really decent so I don't think he'll fuck us over on the exam.
Just have to stop procrastinating now...