Tuesday, November 16, 2010

Simple recipe for trouble (PHP)

1. In a base class, make a method which returns the date in the given format, with some specific text format as default, like this:


        class MyHumbleBaseClass {
   function getDateParam($format="Y-m-d h:m:s") {
              $timestamp = .... 
              if ($format==null) return $timestamp;
              else return $this->formattedDate($format);
           }
        }

2. Override it in some derived class like that:
class MyHumbleDerivedClass {
           function getDateParam() {
              return MyHumbleBaseClass::getDateParam();
           }
        }


3. To add some extra twist, forget that the default format is not null, and do things like that:

if ($derivedObject->getDateParam()<$timestamp) {
    // think that it should work
}
4. Enjoy.

PS In fact, simple overriding will probably not be enough; if you supply the parameters, you will end up calling the parent function. But if you use the base method as a strategy from within your other class, thus shielding the direct access to the base class completely, then the success is practically guaranteed.

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?