08 November 2009

JavaScript Recall

Today I finished my small piece of work with JavaScript. Why JavaScript? Good question, actually. The answer is, be cause it is supported by most (or on all even) browsers and can provide the simplest way to prettify the HTML. When I starting this blog I suddenly stuck with adding a code snippet. The <pre> tag would help, but my code was too long to fit in a predefined place, so, wrapped lines becomes absolutely unreadable. I started with customizing the blog template by CSS and step-by-step I finish with JavaScript.


I did not see it really ages ago and get a real fun to recall this pretty simple scripting language. I said simple? Yes, when I met it first time it was really simple. Simple and boneless (i.e. without strong typing) it was so incredible simple that looks like a toy. Thus, I never expect to see there anything more serious than validation of HTML form's controls. But now, am not going to call it simple. The GWT was, probably, a first bell in my head. (Frankly, GWT is suffice controversial project and there are suspected that "smart" guys from Google badly mimics some aspects of HTTP. But it is, probably, a topic for separate post.) Now I am far away from Web, so touching it occasionally and without good attention. After I tried to play with a JavaScript again, I heard a whole symphony orchestra.

But I am going to fast and digress, I think. As I said I tried to prettify my blog with something that can help demonstrate code snippets in more suitable form. When I stuck to get it working, I, according to old habit from the university, did review of existing solution. So far, I stumbled with Alex Gorbatchev's Syntax Highlighter. When I tried to add it to my blog template, I get another problem: the Google do not mind to host my script files on a BlogSpot. The idea to include scripts in template looks not so graceful and even terrible due fairly sensible amount of additional characters. Thus, I have no other option and going to optimize a bit the original script, with intensions to include it in my blog template.

Scripts were very well documented and I pretty quickly got an idea and starting the code reduction. During analysis, I simplified a lexical analyser and removed some UI and functional stuff that I would never use. I was fairly satisfied by result of optimization and all looks good. However it is not a point of this post, it was more important, I came across the prototyping in JavaScript and had some fun to mimics the classic OOP inheritance. The inheritance of functions, for me personally, sounds crazy. But only first time... Actually, whatever you call differently, it does not change its nature.

So far, please meet my version of a code highlighter. In demonstration purposes, I would like highlight the following code snippet that tests an algorithm that so impress the Joshua Bloch that he call it most beautiful code he had seen ever:

/*
     * Multiplicative inverse calculus test.
     * Tests the calculations of integer multiplicative inverse of specified {@code value} by modulus 232. This algorithm
     * uses Newton iteration and has <a href="http://numbers.computation.free.fr/Constants/Algorithms/inverse.html">
     * amazing proof of its applicability for a multiplicative inverse calculus</a> with only 4 iteration.
     * Used in {@link java.math.MutableBigInteger Java Math lib} for reminder calculation.
     * For details see java.math.MutableBigInteger#inverseMod32(int)
     * Really amazing!
     */
     for (int i = Integer.MAX_VALUE / 2 * 2; i-->Integer.MIN_VALUE; i--) {
        final int inverse = inverse(i);
        if (1 != inverse * i) {
            throw new ArithmeticException(String.format("1 != %d * %d", i, inverse));
        }
     }

It is really interesting code and it is really work.

Btw, when work has been finished and I am going to include this scripts into blog template I found how to host files for BlogSpot absolutely free. So far, I created a file store and uploaded files there. Just mind the "Google Sites" instead of the "Google Pages" if you going to do the same.

No comments: