Archive for the ‘(Демек)Програмирање’ Category

JavaScript namespace

0 Comments

I would like to introduce you to an approach of defining namespace in JavaScript.
JavaScript itself does not have capability of namespacing, but we can use its basic
concepts to create namespace. The idea is to create multiple object that will mimic
namespace, for example:

com.javascript.namespace

Here we have the object com which hold the object javascript and in it the object namespace
in which we can define the objects in the “com.javascript.namespace” namespace.

Fist approach is to create these objects by ourselves like so (JSON):

var com = {
   javascript: {
      namespace: {}
   }
};
 
// our object here
com.javascript.namespace.myObject = {};

We can do this automaticly:

 
NAMESPACE_CREATOR = {
 
   createNamespace: function(namespace){
      var o = null;
 
      if(typeof namespace == 'undefined'){
         // nothing to create
         return;
      }
 
      var tts = namespace.split('.');
 
      eval('if(typeof' +  tts[0] + '== "undefined"){' + tts[0] + '= {}; } o = tts[0];');
 
      for(var i = 1; i < tts.length; i++){
         o[tts[i]] = o[tts[i]] || {};
         o = o[tts[i]];
      }
   }
};

Decopmosed:

   var o = null;

“o” will serv as refernce to the current object in the namespace hierarchy.

   var tts = namespace.split('.');

We need to know all object that we will need to create. But we must create the top-hierarchy object first so:

    eval('if(typeof' +  tts[0] + '== "undefined"){' + tts[0] + '= {}; } o = tts[0];');

Kinda confusing?
Well this is what it will evaluates to:
Let suppose we have this call:

   NAMESPACE_CREATOR.createNamespace("com.js.namespace");

it evaulates to:

   if(typeof com == "undefined"){
      com = {};
   }
   o = com;

So the we have reference to “com” in “o”, and “com” is empty object (if not already defined).

Since we have the top-hierarchy object we can do the following:

   for(var i = 1; i < tts.length; i++){ 
         // o[tts[i]] is same as o.something
         // lets say tts[i] = "js"
         // o[tts[i]] = o.js
         // so, if it is not defined already, we'll define it to an empty object
         o[tts[i]] = o[tts[i]] || {}; 
 
         // switch from o to o[tts[i]]
         // eg. if o=com and tts[i] = "js"
         // o[tts[i]] = com.js
         // so o = com.js
         o = o[tts[i]];
   }

Usage example:

   NAMESPACE_CREATOR.createNamespace("com.js.namespace.myNamespace");
 
   com.js.namespace.myNamespace.myProperty = {msg:"so not cool"};
   com.js.namespace.myNamespace.numberProperty = 1000;

funstufz

Swap two values

0 Comments

a^=b^(b=a);

If anyone cares - this is how it works:

 
/* standard swapping without using third variable, using XOR */
a^=b;
b^=a;
a^=b;
/* works pretty god */
 
 
/* 
 * To make this in one line this is what we need:
   1. a^b^a - this evaluates to b
   2. b = a - this evaluates to a (but also changes the value of b: b <-a)
   1,2 => a = a^b^a; or shorten: a^=b^a; (3)
   if we replace one of the a of the expression (1) with 
   the expression (2) we get: a^b^(b=a) (4)
   3,4 => a^=b^(b=a) (5)
*/
 
 
/* example*/
int a = 10;
int b = 30;
 
printf("Before: a=%d; b=%d;\n",a,b);
 
a=a^b^(b=a);
 
printf("After: a=%d; b=%d;\n",a,b);

Ко ќе нема - напрај си сам! (MySQL Scheduler)

0 Comments

Јебига! MySQL 5.0 очигледно не брка работа.

Потребно е да се изврши некој SQL statement почнувајќи на определено време и тоа да се повторува во некој интервал се до некој момент во иднината. 5.0 стандардно нема некоја ваква функционалност (за разлика од MySQL5.1 каде има EVENT и SCHEDULE).

Па решив да напишам нешто набрзина - half-backed scheduler, додуша обична скрипта (процедура) што извршува некоја работа во определено време и во одреден интервал… Тоа што ни требаше нели?

Бујрум скриптата, ако некој сака да ја користи:

 
DELIMITER $$
-- the procedure will not execute unless:
-- 1. the STARTTIME is before ENDTIME
-- 2. STARTTIME is in future
DROP PROCEDURE IF EXISTS SCHEDULED_MAINTENANCE$$
CREATE PROCEDURE SCHEDULED_MAINTENANCE
(
-- starting time of the scheduled task
IN STARTTIME DATETIME,
-- interval between repeating the scheduled task
IN INTERVALSEC INT,
-- time of termination
IN ENDTIME DATETIME
)
BEGIN
   -- precision interval, will check on every second if the time is right for startup
   DECLARE PRIMARYINT INT DEFAULT 1; 
   -- current time, tough it is not nececary to be kept in variable :S
   DECLARE CURRENTTIME DATETIME DEFAULT NOW(); 
 
   IF  STARTTIME >= CURRENTTIME THEN
      IF STARTTIME < ENDTIME THEN
         PRIMARYLOOP: LOOP
            IF SLEEP(PRIMARYINT) = 0 THEN
               IF STARTTIME >= NOW() THEN
                  LEAVE PRIMARYLOOP;
               END IF;
            END IF;
         END LOOP PRIMARYLOOP;
 
         MASTERLOOP: LOOP
            -- NOTE: the SQL statement comes first to ensure execution on start-time
            -- change the select statement below with your SQL statement
            SELECT 'ANOTHER LOOP ITERATION' AS 'HOORAY';
            -- **********
 
            IF SLEEP(INTERVALSEC) = 0 THEN
               IF ENDTIME <= NOW() THEN 
                  LEAVE MASTERLOOP;
               END IF;
            END IF;
         END LOOP MASTERLOOP;
      END IF;
   END IF;
END$$

Повикајте ја вака:

 
CALL SCHEDULED_MAINTENANCE('2008-10-23 01:41:00',4,'2008-10-23 01:45:00')$$

Ако на некој му брка работа…