Tuesday, November 27, 2012

Python Notes

  1. Python 2.7 not available for direct download for Cygwin. Follow the directions at Python 2.7.x under Cygwin to install it. It assumes you have installed gcc/g++ and make in Cygwin.
  2. This was the clearest explanation of Unicode on Python I could find: Unicode in Python, Demystified.

Wednesday, November 21, 2012

Name Notes


A rectification of browser technology names.


Firefox 17Chrome 23IE 9Opera 12
Layout Engine Gecko WebKit Trident Presto
JavaScript Engine SpiderMonkey (in C++)
Rhino (in Java, not used within browser)
Compiles into byte-code
V8 (in C++)
Compiles into assembly
Chakra Carakan (in C++)

The official name for what is normally called JavaScript is ECMAScript. JavaScript was a trademark from Sun and Netscape. Microsoft called its version JScript. See the Wikipedia article ECMAScript for a history.

Monday, November 19, 2012

Rectification of Names (正名)

齊景公問政於孔子。 孔子對曰:「君君,臣臣,父父,子子。」

Duke Jǐng of Qí asked Confucius about government. Confucius replied "Let the ruler be a ruler; the minister, a minister; the father, a father; the son, a son."

-- The Analects 「論語」, 12:11.

Javascript Note 3

  • In Mozilla, the order of events for keystrokes are
    1. keydown event. At this point only modification keys are recorded.
    2. keypress event. At this point the main key is recorded.
    3. keyup event. At this point only modification keys are available.
    4. Element updated.
    Notice the different events for pressing Ctrl-Shift G and Ctrl-Alt G. It depends on key timing. The various key codes are not ASCII key codes but DOM key codes (see KeyboardEvent). Also Mozilla warns that the keyCode attribute of printable keys in keydown and keyup event handlers should not be used.


    Examples
    1. Notice the keyCode is G even though the charCode is g.
      keydown :ALT keyCode[ (18)], charCode[(0)]
      keydown :ALT-SHIFT keyCode[ (16)], charCode[(0)]
      keydown :ALT-SHIFT keyCode[G(71)], charCode[(0)]
      keypress:ALT-SHIFT keyCode[(0)], charCode[G(71)]
      keyup   :ALT-SHIFT keyCode[G(71)], charCode[(0)]
      keyup   : keyCode[ (16)], charCode[(0)]
      
      keydown :CTRL keyCode[ (17)], charCode[(0)]
      keydown :CTRL-ALT keyCode[ (18)], charCode[(0)]
      keydown :CTRL-ALT keyCode[G(71)], charCode[(0)]
      keypress:CTRL-ALT keyCode[(0)], charCode[g(103)]
      keyup   :CTRL-ALT keyCode[G(71)], charCode[(0)]
      keyup   :ALT keyCode[ (17)], charCode[(0)]
      
    2. Typing apostophe (') (ASCII code 39) corresponds to DOM keycode 222. Javascript's String.fromCharCode translates numeric codes to UTF-8 characters. In UTF-8, 222 corresponds to capital Thorn (Þ) which is a letter used in Old English and still used in Icelandic.
      keydown : keyCode[Þ(222)], charCode[(0)]
      keypress: keyCode[(0)], charCode['(39)]
      keyup   : keyCode[Þ(222)], charCode[(0)]
      
    3. Typing "one" results in
      keydown : keyCode[O(79)], charCode[(0)] value[]
      keypress: keyCode[(0)], charCode[o(111)] value[]
      keydown : keyCode[N(78)], charCode[(0)] value[o]
      keypress: keyCode[(0)], charCode[n(110)] value[o]
      keyup   : keyCode[O(79)], charCode[(0)] value[on]
      keyup   : keyCode[N(78)], charCode[(0)] value[on]
      keydown : keyCode[E(69)], charCode[(0)] value[on]
      keypress: keyCode[(0)], charCode[e(101)] value[on]
      keyup   : keyCode[E(69)], charCode[(0)] value[one]
      
      Notice, the full value of the input field is available only on keyup.
    Code Snippet:
    <html>
    <head>
    <style>
    b { color: blue; }
    </style>
    <script type="text/javascript">
    function msg(e, txt) {
      var mydiv = document.getElementById("result");
      
      var modifier = [];
      [[e.ctrlKey,"CTRL"], [e.altKey, "ALT"],
       [e.metaKey, "META"], [e.shiftKey, "SHIFT"],
       [e.getModifierState("OS"), "OS"], [e.getModifierState("CapsLock"),"Caps"]].forEach(
         function (x) {
            if (x[0]) {
              modifier.push(x[1]);
            }
         }
      );
      var txt = mydiv.innerHTML + "<br/>"
              + txt + ":<b>" + modifier.join("-") + "</b>"
              + " keyCode[<b>" + String.fromCharCode(e.keyCode) + "</b>(" + e.keyCode +")],"
              + " charCode[<b>" + String.fromCharCode(e.charCode) + "</b>(" + e.charCode+")]";
      
      if (document.getElementById("includeValue").checked) {
          txt = txt + " value[" + document.getElementById("maininput").value + "]";
      }
      
      mydiv.innerHTML = txt;
    }
    
    function clearResult(e) {
      document.getElementById("result").innerHTML = "";
      document.getElementById("maininput").value = "";
    }
    
    </script>
    </head>
    <body>
    
    <form>
    <input id="maininput"
           type="text" size="80"
           onkeyup=   "msg(event, 'keyup &nbsp;&nbsp;')"
           onkeydown= "msg(event, 'keydown ')"
           onkeypress="msg(event, 'keypress')">
    </input>
    <input id="includeValue" type="checkbox">Include Value</input>
    <input type="button" value="Clear" onclick="clearResult(event)"/>
    <br/>
    <div id="result" style="font-family:monospace">
    </div>
    </form>
    
    </body>
    </html>
    

Wednesday, November 14, 2012

Javascript Note 2

  1. arguments special variable available in the scope of a function. It contains all the arguments passed to a function. For example,
    function p() {
       ...
    }
    function q(x) {
       ...
    }
    
    p(1,2,3,4);   // arguments.length = 4
    q(1,2,3,4);   // arguments.length = 4.
                  // Ie, the first argument is not swallowed by x.
    

  2. Function.prototype.call(t[, args]*). Calls the function passing along args in a content where this refers to t.
  3. Function.prototype.apply(t, arg). Same as call except the argument is an array. Coming from R, I had thought this meant the function was applied to each element of arg.
  4. Array.prototype.map(f). Applies function f to each element of the array, returning a new array.
  5. Array.prototype.forEach(f). Applies function f to each element of the array but returns undefined. In other words, basically used only for side effects such as printing to screen, writing to filesystem, etc.

Be aware that
1 + "5";   // "15"
"1" + 5;   // "15"
"5" & 20;  // 0 
"5" & 21;  // 5
"1" & "a"; // 0   (Octal of ASCII "1" is 061)
"1" & "b"; // 0

var x = { 1:"b", "5":"e" };
x["1"] == x[1];  // TRUE
x[5] == x["5"];  // TRUE



Tuesday, November 13, 2012

Javascript Note 1

Things I learned while writing a Thai transcription script in Javascript.

  1. The "keys" of an array are the indices!
    var myarray = [ "potatoes" "tomatoes" "celery" ];
    var sample = [];
    for (var x in myarray) {
        sample.push(x);
    }
    // sample = [ 0 1 2 ]   NOT  [ "potatoes" "tomatoes" "celery" ]
    

  2. Primitives are passed by value and the references of Objects are passed by value.
  3. this in a method refers to the object from which the method was invoked. See JavaScript “this” keyword.
    var m = function() {
        this.value = this.value +1;
    }
    var a = { "value":1, "m":m };
    var b = { "value":100, "a":a };
    
    a.m();          // a.value = 2
    b.a.m();        // a.value = 3, b.value = 100
    b.a.m.call(b);  // b.value = 101 
    

  4. Objects in Javascript are associative arrays. To create an object is to create an associative array. One way of doing this is through the use of functions.
    function f() {
       var x = 1;
       
       this.y = 100;
       
       this.addup = function addup(v) {
          this.y = this.y + v;
       };
       
       return this;
    }
    
    var func1 = f();
    var func2 = f();
    
    func1.addup(1);
    [ func1.y, func2.y ];  // [ 101, 101 ]
    
    func2 = new f();
    func2.addup(5);
    [ func1.y, func2.y ];  // [ 101, 105 ]
    
    [ func1.x, func2.y ];  // [ NaN, undefined ]
    

    The last line is strange (Mozilla). I personally think it's a bug. I was expecting undefined. See Which way is best for creating an object in javascript? is “var” necessary before variable of object?.
  5. undefined is not a keyword in Javascript. It is just another variable that generally is undefined. The safest way to test if variable x is undefined is typeof(x) == "undefined". Note
    var a = "welcome";
    var b = { "a":"apple", "k":"kite", "z":"zebra" };
    
    b["b"] == null;       // TRUE
    b["b"] == undefined;  // TRUE
    b["b"] === null;      // FALSE
    b["b"] === undefined; // TRUE
    

    However starting in Javascript 1.8.5, undefined is a global non-writable variable whose type is undefined. So
    var undefined = "trickster";      // doesn't do anything
    
    undefined === undefined;          // TRUE
    typeof(undefined) == "undefined"; // TRUE