Sunday, November 15, 2009

ShowSlow - A jQuery plugin for displaying elements in order, one by one with a delay

Recently I needed the functionality for displaying some hidden divs, in order, one by one, and with a timed-delay between each one.

Thus I wrote this jQuery plugin that does infact this (demo):

/*
 * ShowSlow v1.1 - jQuery plugin
 * 
 *  Copyright (c) 2009-2010 Andreas Grech
 *
 *  Dual licensed under the MIT and GPL licenses:
 *    http://www.opensource.org/licenses/mit-license.php
 *    http://www.gnu.org/licenses/gpl.html
 *
 * http://blog.dreasgrech.com
 */

(function($) {
    $.fn.showSlow = function(options) {
        var opts = $.extend({}, $.fn.showSlow.defaults, options),
        elementCounter = 0,
        $elements = this,
        isStopped = 0,
        isPaused = 0,
        callbackExecuted = 0,
        executeCallback = function(val) {
            if (typeof opts.callback == "function") {
                callbackExecuted = 1;
                opts.callback(getValues());
            }
        },
        getValues = function() {
            return {
                elementsDone: elementCounter
            };
        },
        showNextDiv = function($element) {
            if (isPaused || isStopped) {
                return;
            }
            if (!$element.length) {
                executeCallback();
                return;
            }
            $element.slideDown(opts.slideTime, function() {
                setTimeout(function() {
                    showNextDiv($elements.eq(elementCounter++));
                },
                opts.displayTime);
            });
        };

        return {
            start: function() {
                if (isStopped || elementCounter < $elements.length) {
                    $elements.css("display", "none");
                    elementCounter = 0;
                } else if (isPaused) {
                    elementCounter -= 1;
                }
                isStopped = 0;
                isPaused = 0;
                callbackExecuted = 0;
                showNextDiv($elements.eq(elementCounter++));
            },
            stop: function() {
                isStopped = 1;
                if (!callbackExecuted) {
                    executeCallback();
                }
                return elementCounter; //return the number of elements displayed before stopped
            },
            pause: function() {
                isPaused = 1;
            }
        };
    };

    $.fn.showSlow.defaults = {
        slideTime: 1000,
        displayTime: 500
    };
})(jQuery);

Saturday, November 14, 2009

Vim is like your average text editor...on steroids!

So recently I started using vim after a friend suggested it, and man I have to say, it's like your average text-editor on steroids!

vi was actually written in the 70s (1976), but I just started using recently.  Actually, what I started  I had read a lot about it on Stackoverflow, and last time my friend told me he uses vi when working with C (I, instead, had been using the Code::Blocks IDE); and so I decided to give vim (which is actually an extended version of the vi editor) a go during a *boring* Entrepreneurship lesson at school...and I was impressed and got so immersed in it that I even forgot I was still in a lesson at school.

What really impressed me with vim is the amount of work I can do with fewer keystrokes as possible.

Here is a list of couple of basic commands (from this answer over at stackoverflow) :


y(ank) - copy
d(elete) - delete
c(hange) - change
p(aste) - put from buffer after cursor
o(pen) - start a new line
i(nsert) - insert before current character
a(fter) - insert after current character
w(ord) - moves to beginning of next word
b(ack) - moves to beginning of current word or prior word
e(end) - moves to end of current word or next word
f(ind) - moves to a character on the current line
movement keys you just need to learn: h,j,k,l

^ - beginning of text on a line
$ - end of text on a line
0 - first position on line

most commands can be prefaced with numeric modifiers.
2w - means move 2 words
5h - means move 5 charcters to the left
3k - means move 3 lines up
3fs - means move to the 3rd letter s folling the cursor

modification commands (d,c,y) need to know how much to work on.
dd - delete a line into memory
yy - yank a line into memory
cc - change the whole line
c$ - change from current position to the end
c2w - change the text spanning the next 2 words
3dd - delete 3 lines
d2f. - delete to the second period.

. - means redo the last modification command.
/ - searches for text, and then n(ext) will go the next found occurance. N will go prior.
? - searches backwards through the document.

This is the book I used to start learning vim with: http://modular.math.washington.edu/home/agc/lit/vim/vim_tutorial.pdf

Once I complete the above book, I'll have a go at O'Reilly's Learning the vi and Vim Editors

Greatest Common Divisor and Least Common Multiple functions in Haskell

Recently, I wrote a post about finding the gcd and lcm with C: http://knowledge-aholic.blogspot.com/2009/11/greatest-common-divisor-and-least.html

Since then, I have started Haskell and here is how implemented the "same" functions with Haskell (note that with Haskell, I have to use recursion to find the gcd) :


mygcd :: Int -> Int -> Int
mygcd n m
 | m == 0 = n
 | otherwise = mygcd m (mod n m)
 
mylcm :: Int -> Int -> Int
mylcm n m = div (n * m) (mygcd n m) 

Greatest Common Divisor and Least Common Multiple functions in C

Below are two functions (with a sample application) that calculate the Greatest Common Divisor and Least Common Multiple of two numbers; a,b

#include <stdio.h>

int gcd(int x, int y);
int lcm(int x, int y);

int main()
{
    int x = 156;
    int y = 156;

    int g = gcd(x,y);
    int l = lcm(x,y);

    printf("GCD (%d, %d) : %d\n", x,y,g);
    printf("LCM (%d, %d) : %d\n", x,y,l);
}

int gcd(int x, int y) {
    /*;
        a = qb + r,  0 <= r < b

        a => dividend, q => quotient, b => divisor, r => remainder
    */
    if (x == y) {
        return x /*or y*/;
    }

    int dividend = x, divisor = y, remainder = 0, quotient = 0;

    do {
        remainder = dividend % divisor;
        quotient = dividend / divisor;

        if(remainder) {
            dividend = divisor;
            divisor = remainder;
        }
    }
    while(remainder);

    return divisor;
}

int lcm(int x, int y) {
    /*
        lcm(x,y) = (x * y) / gcd(x,y)
    */
    return x == y ? x /*or y*/ : (x * y) / gcd(x,y);
}

Wednesday, November 11, 2009

An elegant Fibonacci solution with Haskell

Here's a very elegant Fibonacci solution I found in Simon Thompson's book, The Craft of Functional Programming:

--Given a tuple with a fibonacci pair, this function returns another tuple with
--the next two numbers in the sequence
fibStep :: (Int, Int) -> (Int, Int)
fibStep (u,v) = (v, u + v)

--The Definition for the Fibonacci pair
fibPair :: Int -> (Int, Int)
fibPair n
| n == 0 = (0,1)
| otherwise = fibStep (fibPair (n - 1))

--Pass the output of the fibPair function to Haskell's fst function,
--which returns the first item in the given tuple
fastFib :: Int -> Int
fastFib = fst . fibPair

Thursday, November 5, 2009

Cleaning out \Windows\Installer folder correctly in Vista

If you are on Vista, you may have noticed that the %SYSTEMDRIVE%\Windows\Installer folder might get quite huge and take up quite a lot of hard disk space.


Anyway, that folder is used as a cache for installation files and patches and removing those will cause you to not being able to repair or uninstall applications, and in some cases not removing patches or applying new patches to software.


To remove the contents of this folder correctly, download Windows Installer Cleanup Utility and once installed, run the following command with it:

msizap.exe G!

Reference: http://www.theexperienceblog.com/2009/05/16/how-to-clean-out-windowsinstaller-folder-correctly/

Tuesday, October 27, 2009

Syntax highlighting in Microsoft Office Word

To paste highlighted code syntax in Word, just use Notepad++

1. Open Notepad++ and load your source file.  Then select your language from the Language menu:



2. Once your code's syntax is highlighted, go to the plugins menu, choose the NppExport plugin and click on 'Export to RTF':



3. Then your source file will be exported to .rtf format and when you open that in Microsoft Office Word, you will see that your syntax is highlighted.  then obviously copy and paste that source.