Monday, July 26, 2010

little javascript discoveries, aka shame on me :)

Shame on me, after going through this cheatsheet I have realized there were some things I didn't know about javascript:
  •  variables declared with var are declared locally, variables declared without var are not;
  • to check whether the value is undefined, one has to use the following construct: 
    • if (x === undefined)
    • and NOT if (x==undefined) because it will always be true.
They forgot to clearly mention the difference between normal arrays:
  • x = [1,2,3];
and associative arrays
  • x = {'a':1, 'b':2, 'c':3};
though they do refer to those as "hash literals" (the document seems to be a bit old though).

    Friday, May 7, 2010

    Python and Quotes - how?

    More from Python interpreter:
    >>> '"Isn\'t," she said.'
    '"Isn\'t," she said.'
    Does it mean there is no way to have both single and double quotes in a Python literal???

    Update: it seems to be an interpreter bug:
    result = '"Isn\'t," she said.'
    print result
    would produce the expected
    "Isn't," she said.
    after all :)

    python and rounding

    From my Python interpreter (version 2.6.5) where I was playing with calculator:
    >>> price + 0.2342341
    22.234234099999998
    >>> round(_,3)
    22.234000000000002
    Why do I always get the stuff the wrong way?

    Tuesday, April 20, 2010

    The "binary search" script

    For some reason, it could not get pasted there.

    import util.Random
    val toFind = args(0).toInt
    val r = new Random()
    val a = (for (i<- 1 to 100) yield r.nextInt(100)).toList.sort(_<_).toArray
    def binsearch(x:Int, a:Array[Int],first:Int, last:Int):Int = {
        if (last-first<=1) {
            if (a(first)==x) return first
            else if (a(last)==x) return last
            else return -1} else {
                val mid = (last+first)/2
                if (a(mid)>=x) return binsearch(x, a, first, mid)
                    else return binsearch(x, a, mid, last)
            }
            }
    for (el<-0 to 99) println(el+":"+a(el))
    val res = binsearch(toFind, a, 0,99)
    println ("res="+res)

    Tuesday, April 6, 2010

    Some innocent fun with Scala and Swing

    Preamble: For the coming codefest organized by devnology (Dutch programming community) everybody was supposed to write some tetris-like application in the language of their choice. After some painful speculations in which of the existing languages I can appear less ignorant than I am, I have decided to use this as a pretext for learning how Swing framework works, and to remember (a.k.a. learn once more) some Scala that I have tried to learn for a while.

    I ended up installing Scala 2.8 (because the swing representation in Scala 2.7 is very inconsistent and you can't get away from mixing in quite some amounts of java code, which makes is less interesting) and NetBeans (because I wanted some context help as looking into scarce 2.8 docs now and then makes the whole task compatible with trying to read "The Woman in White" in the original language armed only with the course of "technical English" - I did that very long time ago, it proved to be my real starting point on the way to more or less decent knowledge of this human language, but it's not that story).

    In short: it was doable (may be I'll add the details later), took me several days to hack something together, and provided a funny discovery.

    In my MainFrame object I have the following code:
    def doStart = {
      matrix.StartGame
      timer = new Timer
      timer.scheduleAtFixedRate (new TimerTask {
       def run() = {
       matrix.NextStep
       mypanel.repaint()
       }
      }, 0, 1000)
    }
    def doStop = {
      timer.cancel()
      matrix.inGame = false
    }
    def DoShift(a:Move) = {matrix.shiftElem(a);mypanel.repaint}
    reactions += {
    case KeyPressed(src, key, mod, value) =>
     {
     key match {  case Key.Right => DoShift(RIGHT)
      case Key.Left => DoShift(LEFT)
      case Key.Space => DoShift(DROP)
      case Key.Enter => DoShift(FLIP)
      case Key.Up => DoShift(ROTATELEFT)
      case Key.Down => DoShift(ROTATERIGHT)
      case Key.S => if(matrix.inGame) doStop else doStart
      }
     }
    }
     The fun fact: omitting the curly braces in DoShift method made the reactions to the key strokes something like 10 times slower on my machine (Ubuntu 9.10 with 2 GB RAM and NetBeans running in the background). Why?

    Thursday, November 12, 2009

    Virtualization Technology versus VMWare Server :)

    Spent quite some time trying to make VMWare Server run 64-bit system (linux gentoo) on the host with 64-bit processor (and 32-bit Vista, which was actually not the problem but seemed to prevent the VM from detecting 64-bit proc). Luckily, I have found this article which cleared up the mist for me :)

    What I did wrong:
    - I should not have enabled all VT settings. I had Dell OptiPlex 960 with E8400 Intel Core2 Duo processor, and in my BIOS there were 3 settings under Virtualization Support, including the one mentioning "trusted execution". This one should not have been enabled;
    - I should have powered the PC down. Virtualization settings are locked when the machine powers up and simple reboot is not enough to change them!

    One other thing I did now:
    - I have added the setting mentioned in this article:
    hv.enableIfUnlocked = TRUE
    to my VMWare Server configuration file (I have a Vista Ultimate machine and it happens to be found at C:\ProgramData\VMWare\VMWare Server\config.ini).
    May be this also helped, don't feel like disabling it again now when the things work:)

    Tuesday, October 20, 2009

    Just came back from the very interesting talk (which seems to be obliged with its taking place to twitter and to AgileHolland community) by Robert Martin.

    The announced topic was "next programming languages", but the discussion was more broader than that. It was not a specially prepared event, which was just right.

    For me, the most noticeable moments were (in no particular order):

    - Brief introduction to Ruby and Clojure. What a pity I've not been involved with Ruby earlier! Seems like fun. Clojure - at first glimpse, not sure if it's the language for biological humans but who knows... it does seem to be very concise, but the code with minimal syntax is far from being easily understood. Well, may be, those who did program in List think otherwise. I didn't. Wouldn't mind to give it a try though!

    - More about Ruby: the absense of interfaces, or rather, the interfaces reduced to the logical end: every separate function is an interface. Have to think about it. From one side, it seems to be logical. From another side, my feeling is that in this case, you definitely need other methods (like TDD) to ensure you are not getting an unexpected mess, if some object you use does have the method you want to call, but it's not what you think of it!

    - The discussion about Scala which followed afterwards. Everybody present appeared to be very interested! Great that Scala seems to be "in the spot". May be transition from Java to Scala might occur in the same way as the transition from C to C++ occured? Again, who knows?.. The key question in relation to this statement was: "What is that little difference in Scala in comparison to Java which would make it like C++ was to C?"  (For me, the biggest similarity is really in the possibility to more or less painlessly switch from Java to Scala, without the need to immediately introduce big changes... and the little difference is "functionality" - the same way as "object orientation" was in C++).

    - Seeing somebody at hand distance who can quote the number PI up to 200 digits after decimal point...

    Also, the audience (which consisted of an intersection between the Dutch twitter community and the members of Agile Holland) was very enthusiastic and helpful, otherwise the discussion would not be that interesting.

    By the way, what would be the next next step? If the functional languages, as it was also mentioned tonight, are on the raise because of the need to handle concurrency properly, what could happen if - for now still very mystical - quantum computation would prove to be a reality after all? Or will it be considered just a very specific type of concurrency? Will assembly still have a chance then? ;)

    Last but not least: recommended reading: the book "Structure and Interpretation of Computer Programs" (available online here). "This book will explain, why assignments are bad and why concurrency is a disaster, and how we still can handle concurrency if we get rid of assignments", - according to our guest:)

    Now, that's it.