All posts by erk

I love writing, computers, puns... can sometimes be spiritual, or mindful. Life is the biggest experience gift you'll ever get... enjoy the hell out of it!

How to add a body on load function with Javascript

This is an article on how to add a javascript function that will be run when a web page has loaded. We begin by defining a function for running after a page (or actually window) has been loaded:

function bodyOnLoad() {
  ..
  ..
}

And then we’ll do:

window.onload = bodyOnLoad;

However, we also want to make sure our setting of the load event doesn’t remove some other setting. This is done by also keeping any older events.

We store the previous on load event by doing;

var prevOnLoad = window.onload;

And we redefine our bodyOnLoad function:

function bodyOnLoad() {
  prevOnLoad();
  ..
  ..
}

However, we can make the creation of the function and the setting of the event a little bit more effective by doing:

window.onload = function() {
  prevOnLoad();

  ..
  ..
}

You still need to get prevOnLoad before you do that

This becomes even more obvious once we create a function for adding new load events:

function addLoadEvent(func) {
  var prevOnLoad = window.onload;
  window.onload = function() {
    prevOnLoad();
    func();
  }
}

In this way, we can concentrate on creating the new load event outside of the function for adding it to the window.onload.

function myEvent(){
  ..
  ..
}
addLoadEvent(myEvent);

We might even do:

addLoadEvent(
  function (){
    ..
    ..
  }
);

Notice the difference between curly braces “{}” and parenthesis “()”

Finally, we have to make sure there is a load event set for the window before calling it from the new event, so we need to check for this:

function addLoadEvent(func) {
  var prevOnLoad = window.onload;
  if (typeof prevOnLoad != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      prevOnLoad();
      func();
    }
  }
}

Review: Faster, Pussycat! Kill! Kill! (1/5)

“Faster, Pussycat! Kill! Kill!” (IMDB, Amazon) is about three strippers Varla (Tura Satana), Rosie (Haji) and Billie (Lori Williams) out in the desert looking for thrills. They come across Linda (Sue Bernard) and her boyfriend Tommy (Ray Barlow). After killing Tommy, they take Linda hostage. At a nearby petrol station they are tipped off about an old, crippled man (Stuart Lancaster) living in the desert with a fortune. They decide to liberate him of his money. All they need to do is get past his two sons (Dennis Busch and Paul Trinka). However, it turns out the old man has an agenda of his own and most of the cast end up knife stabbed or hit by cars before we reach the end of the movie…

I have to admit, this movie is from 1965, and apparently, the way they acted in the 60ies are just not in my taste.

This film has huge problems with the script. Let us start with the name. When someone calls something “Faster, Pussycat! Kill! Kill!” at least I get an image of some kind of incitement/hunt/stress scene. My imagination was that the three “evil” strippers would incite the poor innocent girl to kill some other poor innocent victim. As it turns out, the script contains very little incitement, actually very little action at all.

The lack of action is, obviously, the main problem of this script. The characters (part from Paul Trinka’s Kirk) are all trying to ”be” instead of ”doing”. The three girls go around and snarl and talk “tough” but sadly enough the script fails them; there are virtually no cruelty or toughness for them to act on, leaving the poor actors to try and ”be” as good as they can, which of course just looks silly. This failure also rubs off on the victim of the story, Linda, making her fear and anxiety seem plastic and phony as well.

The script had me cringing more than once from the outright ridiculous plot turns. Especially the scene at the gas station where the station attendant tip the girls off about the old man, had me thinking of those good old days when I participated in high-school plays.

The script doesn’t give much credit to the human psyche either, and it utilizes the “linear cause-effect” type of psychology. For instance, the old man lost his ability to walk when he saved a girl from being hit by a train, so obviously, now he hates trains and girls…

If you want to see how to write a script that will kill all the characters mercilessly, then watch this flick (and by “kill” I don’t mean “blood-bath”-kill, unfortunately, even splatter would have made this less painful… most of the characters die, that’s true but they’re dead long before that happens). Or if you just feel that the good old classics has to be good, old and classic, then watch it and suit yourself! ;o)

I give “Faster, Pussycat! Kill! Kill!” 1 point of 5. (I’m not gonna f-up my own system by starting to deal out zeroes and minuses but I am pretty tempted…)

Programming humor

In case you wondered. Sure, programming can be humorous, but this is more about looking at programming with humor. Or, well, I’ve found a few funny things I’d like to share… XML is like violence: if it doesn’t solve your problem, you’re not using enough of it.

People who make buttumptions about their censoring settings, will be embarbutted when they repeat this clbuttic mistake.

Review: Paycheck – Let the future be untold (4/5)

Paycheck (IMDB, Amazon) is a story about Michael Jennings (Ben Affleck) who is an engineer, or to be more precise a reverse engineer. Michael is paid to take competitor’s work and reverse engineer it into something his employees can make into a products of their own.

Since it would be very bad if information about whose technology was reverse engineered into what, Michael’s assistant Shorty (Paul Giamatti) helps removing all of Michael’s memories of the project once work is finished.

A once in a lifetime opportunity comes along as Michael’s old friend Rethrick (Aaron Eckhart) offers him work that will give him stocks in Rethrick’s promised-to-become-great company. Michael takes on the three year project, even if he risks losing his memory for the whole period, and that of a probably blooming romance with one of Rethrick’s employees, doctor in biochemistry, Rachel Porter (Uma Thurman).

Three years passes, Michael finds himself back where he once begun, in Rethrick’s office, his memory wiped and all that stands between him and his millions, a trip to the bank.

That is however, when problem starts, because Michael finds not only has he switched the personal effects he once had to leave before entering Rethrick’s employee, he has also forfeited a 100 million dollars worth of stocks in Rethrick’s company.

Why did Michael say no to the money and, of significantly less importance, what became of his personal effects? Michael soon realizes his former employees and the FBI are out to get him, and his bag of assorted effects seems to be the only thing that keeps him ahead of the game. A game, that if lost, could cost him his life…

You can read the whole review by clicking the below link, but there may be spoilers in that text…

Continue reading Review: Paycheck – Let the future be untold (4/5)

Programming booleans

When looking at other programmer’s code I’m sometimes surprised with things like this:

exportDocumentView(java.lang.String absPath, java.io.OutputStream out,
boolean skipBinary, boolean noRecurse)

And in such prominent frameworks as Java Content Repository as well.

What’s my problem then? Double negations. In order to do exportDocumentView and get binary data and recursive export you’ll need to do:

exportDocumentView(“/path/to/my/Node/”, System.out, false, false)

Sure I can handle it… but… false to opt something in? I find it rather fishy. Call me an idiot but my brain just don’t deal with that kind of stuff easily…

What I would have wanted instead was:

exportDocumentView(java.lang.String absPath, java.io.OutputStream out,
boolean includeBinary, boolean recursive)

Which would have been called like:

exportDocumentView(“/path/to/my/Node/”, System.out, true, true)

For when we want binary data and recursive export, and like this for the case when we don’t want either or both:

exportDocumentView(“/path/to/my/Node/”, System.out, false, true)
exportDocumentView(“/path/to/my/Node/”, System.out, true, false)
exportDocumentView(“/path/to/my/Node/”, System.out, false, false)

I’m just saying. In my world false means “no” and “no” means don’t give me something or don’t do something:

“Don’t return binary data.”
“Don’t recurse the tree of nodes.”

To be compared with:

“Don’t skip binary data.”
“Don’t do no recursing.”

(But you’re free to curse? :o)

He heard her own gasp of astonishment

I just read the following sentence:

“He heard her own gasp of astonishment”

Some words just don’t work well with things no one else can do for you, I mean…

I entered the room and guess if I was surprised. My neighbor gasped with my astonishment, and as he had one of my thoughts, he came over to my own apartment to have a talk with me. He told me, with his wife’s voice, he disliked all but his own ideas. I had one of his ideas and in a contrary to his position, decided I liked it, so I made it my own idea.

It could get pretty interesting after a while of that. Perhaps the next pop-style of writing…?

Review: Ultraviolet – Xenophobia (3/5)

Ultraviolet (IMDB, Amazon) is a story about Violet, an ordinary woman whose life changed when she was infected by a virus that turned her into a feared and hated hemophage.

Once contracting hemophagia, Violet was incarcerated and experimented on, and perhaps these experiments cost her the child she was bearing when infected, perhaps the infection itself did, regardless, Violet escaped and now she is out to steal the government’s latest and most deadly weapon in the fight against hemophages.

The hemophages have superhuman strength and speed, but at a cost; few live longer than a decade. Since the virus that causes hemophagism infect on blood contact, the number of hemophages should have grown had it not been for the government’s prosecution.

Like vampires the hemophages have pointy eyeteeth, but unlike the vampires they do not require sucking blood and taking lives to survive. They are far from demons, and rather unfortunates infected by a deadly and infectious disease, and the only demon thing about them is the demonizing of them done by the government.

You can read the whole review by clicking the below link, but there may be spoilers in that text…

Continue reading Review: Ultraviolet – Xenophobia (3/5)

DVDs catching up?

Once upon a time a CD was large. Huge. You used it to save lots and lots of data.

Then came MP3s, movies, digital cameras with millions of pixels and suddenly the CD was small. And the DVD came, and for a while it was large… I don’t think it ever was huge.

Today I would have to use more than 300 4.4 GB DVDs to save all my data, and that only includes the data on hard drives. Making back-ups becomes a question of selecting what to back-up and what to leave for chance. A real pain.

But there might be light at the end of the tunnel.

Researches at the Technical University of Berlin claims they’ve managed to put up to 500 GB of data on a CD/DVD-sized disc

This is (layman’s interpretation) done by using holograms. The disc is transparent (wonder how that will work with labeling?) and uses ten layers of data (to compare with blu-rays that use two, and ordinary DVDs that use one). Anyway, sometime around 2010 they might be able to fit as much as 1 Terabyte to a CD-sized disc.

That’s probably the same time as hard drives comes in 10-20 Terabyte sizes, and soon after the 1TB DVD will once more be insufficient.

Wheehoo…

Evolution — a guessing game…

Does evolution really work? Has it really made us what we are today? Surely the chance of just randomly creating human life must be “zero many times over”, right?

I’ve read quite a few pages from the more fundamental and lively bunch of Creationists and Intelligent Design aficionados (like for instance this one), and I’ve frankly grown tired of this talk about randomly creating life…

Let me try to explain.

As far as I’ve understood, opponents to Evolution uses two arguments:

  1. Life (humans, humans from apes etc) cannot have happened by chance, neither can evolution from once specie to another happen by chance.
  2. The probability for spontaneous evolution from one specie to another is “zero many times over”

Let’s begin with 1: Life cannot have been created by chance

Not even Darwin says life was created haphazardly or by random. He says life was created by “survival of the fittest”. This means nature gives feedback to life (maybe there’s where we should go look for God?), mostly by killing off those who wasn’t fit enough (which sounds like we’re looking for Old Testament God, right?)

Theories of evolution has never claimed life was created by chance

Let’s compare life developing by survival of the fittest to a guessing competition. Say between our friends Craig and Eve… Craig will think of a number between zero and, oh why don’t we take 30,000,000,000 (which happens to be the number of nucleotides in human, DNA, but for now it’s just a number), and Eve will try to guess it. Craig will only tell Eve if her guess is too low or too high. Eve will get as many attempts as she needs. Now… how many attempts does she need? Well it won’t be like twenty questions, not with 30 billions in the pot…

The actual formula for the number of attempts in this kind of guessing game is:

Formula for max guesses = log10(n_max) / log10(2)
Formula for max guesses

And for guessing a number between 0 and 30 billion:

max number of guesses needed to guess a number between 0 and 30 billion
max number of guesses needed to guess a number between 0 and 30 billion

So… she needs 35 tries. I think 35 tries to guess a number between 0 and 30 billion isn’t unfair…

So, it stands between Craig and Eve then:

Eve:   Think of a number between zero and thirty billion.
Craig: Ooops... okay lemme think.
            (But instead Craig goes to random.org,
            and gets the number, 16,637,597,566)
         Okay.
Eve:   Is it 15,000,000,000?
Craig: Higher.
Eve:   Is it 22,500,000,000?
Craig: Nah, lower.
Eve:   Is it 18,750,000,000?
Craig: Nah, lower.
Eve:   Is it 16,875,000,000?
Craig: Nah, lower.
Eve:   Ah, darn.  Is it 15,937,500,000?
Craig: No.  Higher!
Eve:   Aha!  Is it 16,406,250,000?
Craig: Nope (hehehe).  Higher.
Eve:   I'm sure its 16,640,625,000!
Craig: Millions of numbers wrong!  And lower.
Eve:   Okay.  16,523,437,500?
Craig: Nope.  Higher.
Eve:   Sheesh!  16,582,031,250?
Craig: Nah, Lower... you're flailing allover the place!
Eve:   (Blushes).  Okay, is it 16,611,328,125?
Craig: No. Higher!  (You'll nevva get it...)
Eve:   Is it 16,625,976,563?
Craig: No. Higher!
Eve:   (Sigh!)  Is it 16,633,300,782?
Craig: No. Higher!
Eve:   Oh Shoot!  Is it 16,636,962,892?
Craig: No!  No!  No!  Hiiiiigher!
Eve:   Is it 16,638,793,947?
Craig: Hahahah!  You're sooo lost!  Lower!
Eve:   Is it 16,637,878,419?
Craig: Lower!
Eve:   Is it 16,637,420,655?
Craig: Higher!
Eve:   Is it 16,637,649,537?

Well, those of you who hasn’t realized by now that Eve will manage to nail that number between zero and thirty-billion before long… … read on.

So what happened here? I knew the number beforehand, so I just arranged the guessing competition?

This is exactly what happened:

Eve started off with half the top number (half of 30 bil is 15 bil, right?) And depending on if Craig told her the number she guessed was higher or lower, Eve divided 15 bil in half (7.5 bil) and added or subtracted to 15 bil. In this scenario 15 was lower than 16 so Eve added 7.5 and got 22.5 bil which was way high. Craig told her so, Eve divided 7.5 in two (getting 3.75) and subtracted this from 22.5, getting 18.75). (You can see exactly how this plays out in [1] below).

You can do this as well, but if you don’t have a calculator and pen and paper, choose a lower max, like a thousand or ten-thousand.

Okay, back to the discussion. First off, the higher-lower clue is probably more than nature gives us (it just kills us ;o). And guessing between zero and 30 bil is probably also grossly underestimated. The number should probably be thousands, even millions of times higher, and the clues about as much fuzzier.

So, let’s up the stakes!

How many attempts would it take for Eve to guess a number roughly representative of the human genome? There are some 3 billion base pairs (Functional and Comparative Genomics Fact Sheet) in a human genome. Each of these pairs can consist of a combination of four different bases (usually abbreviated A, C, G, and T for adenine, cytosine, guanine, and thymine, http://www.answers.com/nucleotide), but for the heck of it, let’s assume once we’re done understanding life we’ve realized there’s as many as 100 different bases that can be combined in a pair, and that they can be combined in any order.

Let’s represent a pair as two numbers between 0 and 99 for each base, meaning one pair is a number between 0 and 9999. So, the number we’re looking at is 3 billion x 4 digits long. Minimum 0, maximum 12 billion 9’s. We can describe this number as a power of ten:

The number representing the genome
The number representing the genome

And the maximum number of attempts needed will be:

Number of guesses for the genome
Number of guesses for the genome

That means it will take approximately 3.32×10^13 attempts to get to the right human genome (and that is probably exaggerated since this calculation first says there can be as many as a hundred bases in a nucleotide pair — space taken for future exploration, and no regard has been taken to what nucleotides really can be paired together with which. Also note that this number of attempts are required for a given individual not just any human. If Eve guessed on Craig’s grandpa instead of say Craig, she would still be wrong and have to continue until she guessed on Craig).

If one person were to make one guess per second on an number “roughly” equivalent to the human genome it would take about 1.05 million years to guess the correct number…

What does this number mean? If we were to make one attempt a second it would take us roughly 1,053,000 years to get the right number. There are a lot of factors to take into consideration here. Most of the groundwork was made before nature even managed to produce the first single-cell organism… the number of failures before this happened were probably numerous and recurrent.

Some scientists (Functional and Comparative Genomics Fact Sheet) believe the differences between mammals to be very small (for instance the genes in a mouse not possessed by a human and vice versa may be around 1%). What this probably means is that there are numerous combinations which has never occurred within any animal on earth. Already from the beginning a huge number of combinations were discarded to the benefits of others (or in other words, the kind of life we’re seeing on earth is probably far from the only working combination!)

The kind of life we’re seeing on earth is probably far from the only working combination!

Some bacteria reproduce in about an hour (but rates can vary between 12 minutes and 24 hours or even more http://textbookofbacteriology.net/growth.html), so for those bacteria it would take 3600 times as much time to get the exact genome down…. and that means it would take 3,8 billion years… how… convenient, since scientists believe life occurred on earth some 3.2 – 3.7 billion years ago (How and Where Did Life on Earth Arise?, Carl Zimmer, Science, Vol 309, Issue 5731). However, when talking about bacteria, we’re not talking about one single line of bacteria doing the whole job, we’re talking about billions and billions of them working in parallel, one variant defeating another less “fit to survive” variant, over and over again.

It’s actually a bit hard to see how life could have not evolved, given the circumstances on this planet, but let’s not go further into that duscussion just yet.

Okay, point number 2 above. The probability for this to happen is “zero many times over”

Well, maybe you would have been right if Earth was the only place were life has attempted to evolve. However… you need to take a good look at the universe… (unless you belong to those who think the Sun rotates around the Earth and the stars are small piercings in a black cloth, if that’s the case, I can only give my condolences….)

In the Milky Way alone there are some 200 billion stars (see Ask an Astrophysicist, Size of the Milky Way), and even if only 1% of these have planets, and 1% of those has planets with even a remotely acceptable temperature and only 1% of those have an atmosphere that would sustain life and only 1% of those really had life and only 1% of those had intelligent life it would still be 20 stars in this galaxy alone with intelligent life.

That would equal a 1 in 10 billion chance of intelligent life (0.000000001%).

Given that the universe is estimated to contain some (holes in black cloths disregarded here, sorry) 1,000,000,000,000,000,000,000 stars (10^21) (see Ask an Astrophysicist, The Number of Stars).

The universe is estimated to contain some one thousand billion-billion stars (that’s a one with 21 zeros after it…)

The 1 in 10 billion estimate would give us around 100,000,000,000 (100 billion) star systems with intelligent life in the universe… The part of the universe we can see so far…. However, I believe that number to be grossly exaggerated, I am sure there are no more than say three or four thousand planets with intelligent life in the known universe (which would give us a 1 in 285 million billion chance — or 0.00000000000000035% — for intelligent life in a given solar system).

I believe there are only a few thousand solar systems with intelligent life in the universe…

Very few of these 3-4000 planets probably have societies that has evolved far enough to start making noise we can pick up. Some may take thousands of years before they get that far, some may already have gone past the “radio” era, but since they’re millions of light-years away they would already have come here, seen us, yawned and went home again, when their radio signals finally reaches us…

One of the major failures of ID/Creationism aficionados is the inability to fathom how huge the known universe is. Professor Edwin Conklin is rumored to have said that the probability of life originating from accident is comparable to the unabridged dictionary resulting from an explosion in a printing factory. To put things into perspective, lets contemplate the number of printing factories that can be placed on the planets of one thousand billion billion solar systems, and how many times these factories can explode during some 3.5 billion years.

I am still saying life did not happen by chance, accident or randomness, but I’d like to make a point that the size of the universe does play a significant role when determining the feasibility of evolutionary theories.

So what do I believe….?

I believe if God ever created anything, then God created evolution… >:P

Maybe God created evolution?


[#1] The number guessing, beat-by-beat (Craig thought of the number 16,637,597,566)

Guess # Eve’s guess What she added / subtracted Craig’s reply
1 15 000 000 000 Too low!
2 22 500 000 000 7 500 000 000 Too high!
3 18 750 000 000 3 750 000 000 Too high!
4 16 875 000 000 1 875 000 000 Too high!
5 15 937 500 000 937 500 000 Too low!
6 16 406 250 000 468 750 000 Too low!
7 16 640 625 000 234 375 000 Too high!
8 16 523 437 500 117 187 500 Too low!
9 16 582 031 250 58 593 750 Too low!
10 16 611 328 125 29 296 875 Too low!
11 16 625 976 563 14 648 438 Too low!
12 16 633 300 782 7 324 219 Too low!
13 16 636 962 892 3 662 110 Too low!
14 16 638 793 947 1 831 055 Too high!
15 16 637 878 419 915 528 Too high!
16 16 637 420 655 457 764 Too low!
17 16 637 649 537 228 882 Too high!
18 16 637 535 096 114 441 Too low!
19 16 637 592 317 57 221 Too low!
20 16 637 620 928 28 611 Too high!
21 16 637 606 622 14 306 Too high!
22 16 637 599 469 7 153 Too high!
23 16 637 595 892 3 577 Too low!
24 16 637 597 681 1 789 Too high!
25 16 637 596 786 895 Too low!
26 16 637 597 234 448 Too low!
27 16 637 597 458 224 Too low!
28 16 637 597 570 112 Too high!
29 16 637 597 514 56 Too low!
30 16 637 597 542 28 Too low!
31 16 637 597 556 14 Too low!
32 16 637 597 563 7 Too low!
33 16 637 597 567 4 Too high!
34 16 637 597 565 2 Too low!
35 16 637 597 566 1 *growl*

Using UUIDs to Prevent Broken Links

I don’t know if someone has proposed this before, but how about using UUIDs to prevent broken links?

A UUID, Universally Unique Identifier, is a hexadecimal number divided into five sections. A UUID has the special quality that it is universally unique. This means two people on each side of the world could create a UUID each at the exact same time, and still be sure their UUIDs are not identical. In fact they can create a large number of UUIDs and still be sure they are not identical. (The same goes for two people on the same server.)

This quality makes UUIDs a perfect tool for assigning unique IDs to web pages or other Internet resources (in fact any resource of any kind, your dog, the cuttlery in your drawers, you name it.)

This could be done like this:

Step 1: Place the UUID on the page

First a UUID has to be put on the webpage, perhaps with a meta-tag, or with plain text on the page.

With a meta tag it could be done like:

<meta name=”UUID” content=”8523813a-7c47-4cd9-ad78-09c14dfb505f”/>

Or on the page, like:

UUID: 8523813a-7c47-4cd9-ad78-09c14dfb505f

Step 2: Find the UUID

The second step would be to make sure every time a program stores a URL to the page it also stores the UUID. (When creating bookmarks, or linking from one site to another etc).

So, once the page get lost, either because the link has changed, the page has been moved or something similar, the browser (or site) can use the UUID to find the page again.

The second step obviously demands a search engine (or some other central registry) that utilizes UUIDs in it’s index since the system does require some kind of central processing for keeping track of a UUID-to-page-link.

A UUID is not a particularly good URI since even UUIDs generated at the same host just a few seconds apart are still totally different from each others (this actually depends on implementation, but one should not assume UUIDs from the same host shares any similarities).

This however is also one of the strengths of UUIDs since it means an Internet resource should be possible to locate regardless of its physical location (in a contrary to ordinary http-URLs that are tightly bound to their location — they start with the server name).

Since a UUID (per definition) is universally unique, it is fairly simple to generate one wherever you are, and use it in a page, be sure there are no duplicates and locate the exact page of the UUID again.

A Google experiment

I’ve placed the text “UUID: 8523813a-7c47-4cd9-ad78-09c14dfb505f” on this page. (Several times now). As far as I’ve been able to discern, Google indexes even such arbitrary information as UUID data (the exact string “8523813a-7c47-4cd9-ad78-09c14dfb505f” to be precise, check out this page with a discussion on how to use UUIDs to make pages unique… It has nothing to do with this discussion but is an interesting example on how UUIDs could be used with Google).

By searching for “8523813a-7c47-4cd9-ad78-09c14dfb505f” it should be possible to locate this page… (see if it works? — Give Google time to index the page though… Update: the above link seems to not work, but this one [searching for the UUID with “-” replaced to space — or “+”], however, does…)

Finally

The page localization should work regardless of the page’s position, site, or anything. In fact, as long as the UUID is still there, it should even be possible to place this text in a document of type Word/OpenDocument/PDF or any other format a search engine can index, and the text would still be possible to find with nothing but the UUID.

Obviously the end result of this technology would be that there is no “search-engine-in-between” but instead whenever the link is lost, the caller goes to the central repository/search engine (or some other place) and locate the page, then links to it automatically. It should even be smart enough to retry until it finds a link that works if a UUID has several possible links.