Loki's blog

  • Home
  • maloki.net
  • Ubiquity 0.5; Updated Ping.fm command

    • 10 Jul 2009
    • 0 Responses
    •  views
    • Programming Technology projects
    • Edit
    • Delete
    • Tags
    • Autopost
    Ubiquity 0.5 is out! If you currently have and older version of Ubiquity installed for Firefox, it won't automatically update since it would break a lot of other commands in the wild. If that's ok with you, you can download Ubiquity 0.5 at Planet Ubiquity. Of course, mine broke, too, and I just updated it! Whoo! I updated my Ping.fm Ubiquity command to use the new parser. Others should also be able to localize the command if they want to do so*. The old version of the command is still up just in case some issues pop up in this version of the command. There are still some tweaks (and maybe some other features) I want to put in but this is it for now. Time for me to get some sleep. It'll be a busy day at work later! -- *Well, not now, but probably in the future as:
    "Community commands (those hosted on individual servers, locally, or on the herd), however, cannot be localized at this time." (via: Labs/Ubiquity/Ubiquity 0.5 Making Commands Localizable - 09.07.10)
    Maybe soon...
    • Tweet
  • Don't Repeat Yourself using LINQ & Delegates

    • 29 May 2009
    • 0 Responses
    •  views
    • C# DRY LINQ Programming Technology
    • Edit
    • Delete
    • Tags
    • Autopost

    In one of my projects, I had to look for certain objects using certain qualifiers: ID, Name, ID & Name, etc… Without optional parameters (Hello C# 4.0!), I ended up with copying & pasting whole blocks of code: initializing, filtering, returning the result, error handling, etc. I wondered if there was an easier solution to that and then it hit me! LINQ & delegates!

    Let’s start with a simple class:

    public class Employee
    {
        public string FirstName { get; protected set; }
        public string LastName { get; protected set; }
        public int Id { get; protected set; }
        //...and other methods...
    }

    Then, we have a List of Employees:

    List<Employee> Employees = new List<Employee>()
        {
            new Employee("Juan", "dela Cruz", 1),
            new Employee("John", "Doe", 2),
            //and so on...
        };

    Now let’s try making some convenience methods for retrieving a collection of filtered Employees.

    public IEnumerable<Employee> GetByLastName(string last)
    {
        IEnumerable<Employee> result = 
            Employees.Where(emp => emp.LastName == last);
        return result;
    }
    
    public IEnumerable<Employee> GetById(int id)
    {
        IEnumerable<Employee> result =
            Employees.Where(emp => emp.Id == id);
        return result;
    }

    Quick & easy, yes? Now what if instead of a List, our collection of Employees isn’t that stable. We’d need to put each of them in try-catch blocks and add error handling. This can quickly become high-maintenance code.

    Enter LINQ and delegates!

    We then basically need one method that will contain all the error handling, initialization and whatnot. We just pass a delegate will filter the collection:

    public IEnumerable<Employee> GetEmployee(Func<Employee, bool> query)
    {
        try
        {
            //initialization
            IEnumerable<Employee> result = Employees.Where(query);
            return result;
        }
            //handling other exceptions...
        catch (Exception ex)
        {
            //Error handling
            return null;
        }
    }

    Did you notice: Func<Employee, bool>? All we need is to pass an expression that uses the Employee as input and returns a boolean (true if it passes the condition or false if not).

    What does this mean? Well, we can now replace the convenience methods we wrote earlier with this:

    public IEnumerable<Employee> GetByFirstName(string first)
    {
        Func<Employee, bool> query = emp => emp.FirstName == first;
        return GetEmployee(query);
    }
    
    public IEnumerable<Employee> GetById(int id)
    {
        Func<Employee, bool> query = emp => emp.Id == id;
        return GetEmployee(query);
    }

    We’re back to code that’s easy to maintain with all the initializations, error handling and other code blocks that you need.

    You could even do away with the convenience methods and use lambda expressions all the way!

    GetEmployee(emp => true); //get everything
    GetEmployee(emp => emp.LastName.Contains("Locs"));
    GetEmployee(emp => emp.FirstName == "Richard"
        && emp.LastName == "Locsin");

    Have fun!


    Related resources:

    • LINQ
    • Lambda Expressions
    • Tweet
  • Inverse Square Root

    • 5 Jan 2009
    • 0 Responses
    •  views
    • Programming Share Technical Technology gaming geek
    • Edit
    • Delete
    • Tags
    • Autopost

    I just bumped into a clever little code snippet used to find the inverse square root:

    float InvSqrt(float x) {
        float xhalf = 0.5f * x;
        int i = *(int*)&x;   // store floating-point bits in integer
        i = 0x5f3759d5 - (i >> 1);  // initial guess for Newton's method
        x = *(float*)&i;  // convert new bits into float
        x = x*(1.5f - xhalf*x*x);  // One round of Newton's method
        return x;
    }

    [ taken with comments from Kalid Azad @ BetterExpalined.com: Understanding Quake’s Fast Inverse Square Root. ]

    Ok, it does not get the exact value but you get a good enough estimate, FAST!

    There are two important things to note here:

    • Newton’s method (also known as the Newton-Raphson method)
    • The ‘magic number’: 0x5f3759d5, an optimized first guess

    For a better explanation, I suggest reading Kalid Azad’s article and the paper by Charles McEniry.

    Wait a minute, so why is this useful?

    In graphics programming, this can be used to normalize vectors quickly. If you noticed the title on the first link, it’s used in the Quake 3 engine. So does that mean the legendary John Carmack wrote this code? If you’re interested, you can read the Origin of Quake3’s Fast InvSqrt() at Beyond3D.

    What did I learn?

    I usually forget one thing: there are times when speed trumps accuracy. A close quick estimate would be better and there are a number of optimations that can be made. Much thanks goes to Doc Mana who first introduced me to the Newton-Raphson method & how to apply it on code. 

    • Tweet
  • Quadrox: Yet another Tetris clone

    • 10 Nov 2008
    • 0 Responses
    •  views
    • Personal Programming gaming projects tetris xna
    • Edit
    • Delete
    • Tags
    • Autopost

    I had to make one myself. I just had to. I just started last Friday and it didn't take long to get it going with XNA. I just wanted it to behave similar to Tetris DS + other rules I picked up from Heboris, and that took some time... (plus, I wanted to watch Madagascar 2 & Quantum of Solace, too, and did. :D). If you want the game, it's here: Quadrox v. 1.1. Quadrox v. 1.1.1.

    Installation:

    • Unzip Quadrox_v1.1.zip into a temporary folder (doesn't really matter where, as long as you remember where it is).
    • Open that folder and run "setup.exe" and continue with the installation.
    • If you don't have the prerequisites ( .NET & XNA frameworks, the setup will download them)
    • When it's done, it will automatically run the game in windowed mode.
    • I'm not sure but I think the temporary folder can be deleted at this point...

    Uninstallation:

    • It can be found in the Add/Remove programs in XP and in Vista's counterpart (not sure what it is, sorry).

    Just a summary on the project:

    Motivations

    • Something to play while waiting for a download job. (Does not require an internet connection & can just pick up and play)
    • Tetris with my desired ruleset (at least attempt to get it)
    • Simple UI that does not distract you from the game
    • Practice C# and using XNA :P
    • Non-intrusive app
    • Lightweight (well it is, except for the need for the .NET & XNA frameworks... )

    Features

    • Can select starting level: 1-30
    • Can spin the current block while it hasn't been locked.
    • Can hold blocks
    • Always in "Endless mode" - You don't stop after reaching level N.
    • Shows you your next 7 blocks = more strategy, less luck.
    • Can run in windowed or full screen mode
    • "Secret" INVISIBLE MODE - toggled at the Game Over screen (F9). Well, not a secret anymore...

    Known Bugs:

    • Rotation when trying to slip in some bricks does not behave as intended. (Thus some S/Z/T-Spins might not be possible)
    • Starting position of some blocks may seem lower or higher than others.

    Future of the Project:

    • Add pre-spinning blocks before the blocks spawn on top.
    • Add ability to pause the game - hahahaha!
    • [Change UI]
    • [Might add some sounds]

    If you find any bugs, have comments/suggestions, please leave a comment or contact me using the Contact link. Thanks.

    • Tweet
  • Accounts

    • 22 Jul 2007
    • 4 Responses
    •  views
    • Complaint Ideas Programming Technology
    • Edit
    • Delete
    • Tags
    • Autopost
    I just realized today that I have more than 30 accounts and service subscriptions spread across the internet. Hmm, is it actually possible to create an interface that would unify all these and act as a hub?  Possible features:
    • Have an overview of all accounts: A screen full of widgets (Hooray for info overload!)
    • Track how often each account has been used: This will help the user decide if that account should be disposed of
    • Possibly integrate feeds...but then, the user'll probably have another service to handle that so it's not important
    • List all/most (or at least the most popular) services on the internet
    • Social aspect
      • Rate services
      • View top services
      • View new services
      • View services in open/closed beta
      • Suggest services to friends
    • Possible interface between services...but not that important since other people has mostly done work on this already and a lot of services already have their APIs public.
    If OpenID or something similar was implemented long ago, this would have been easier.
    • Tweet
  • Decimal Format in C/C++

    • 3 Mar 2007
    • 4 Responses
    •  views
    • Programming
    • Edit
    • Delete
    • Tags
    • Autopost
    Recently, we had a project in C++. One problem I encountered was to properly format a double so that it displayed two decimal places. I'm used to programming in JAVA and what I'd do is to use the DecimalFormat class. In C/C++ there doesn't seem to be a solution, or at least an obvious one. Without the DecimalFormat class, I would have either: separate the whole number and decimal values and just insert a decimal point inbetween. Or, search for the decimal point and either add trailing zeroes as needed or truncate it to two decimal places. Doing that in C++ seems quite long. I could do that but it seems a bit crude. In the end, I did this: #include #include using namespace std; string moneyf(double d) { char temp[50]; sprintf(temp, "%0.2f", d); return string(temp); } It works and it's shorter. In C, you'd include stdio.h instead of cstdio and return a char[]. Maybe this is the solution, maybe there's an even less crude way to do this. If you have any suggestions, they're very welcome :D
    • Tweet
  • Permutation

    • 14 Mar 2006
    • 0 Responses
    •  views
    • Education Programming
    • Edit
    • Delete
    • Tags
    • Autopost
    We were asked to make a program (in Python) that finds all possible permutations of a set of elements [1,2,...,n], where n is a user input. I started off by writing a permutaion of elements [1, 2, 3, 4, 5, 6], studied it, and wrote my algorithm. I ended up with one function that looks for the next lexicographic permutation:
    Media_httpstaticflick_vctal
    Also, I had to come up with a little sort fuction (I used bubble sort) to fix up the elements inside:
    Media_httpstaticflick_jieju
    What bothers me sometimes is what I sometimes forget why the sort method works for the permutation. Sometimes, I have to check out the code again and explain it to myself x.x Kind of pathetic, but I get kind of confused with my own code at times. Sometimes, I feel like I either type faster than I think so I get lost, or type slower than I think so I forget. Either way, I just hate it when it happens...
    • Tweet
  • JAVA

    • 16 Jan 2006
    • 0 Responses
    •  views
    • Programming
    • Edit
    • Delete
    • Tags
    • Autopost
    It's quite annoying. I spent half a day trying to figure out what was going wrong. I uninstalled & reinstalled everything related to Java (except the IDEs) and still encountered the same problem. What problem? Stuff won't load. Applications that use Java just display the title. If I close the window, my computer crashes. Eventually, I was able to trace the problem back to new display drivers I installed the other day. At first I thought that the rolling back the display driver (from ForceWare 81.98 back to 78.01) won't do a thing but well, it did! Now, I can go back to programming...
    • Tweet
  • About

    25-year old Filipino software developer and gamer who loves food, especially cookies. Mmmm, Cookies!

    28050 Views
  • Archive

    • 2011 (1)
      • May (1)
    • 2010 (1)
      • September (1)
    • 2009 (8)
      • July (1)
      • May (1)
      • April (2)
      • January (4)
    • 2008 (17)
      • December (1)
      • November (3)
      • September (3)
      • July (6)
      • June (1)
      • April (1)
      • January (2)
    • 2007 (24)
      • November (2)
      • October (2)
      • September (3)
      • August (4)
      • July (6)
      • June (2)
      • May (2)
      • March (2)
      • February (1)
    • 2006 (10)
      • November (3)
      • March (1)
      • February (2)
      • January (4)
    • 2005 (5)
      • December (1)
      • November (2)
      • March (1)
      • January (1)
    • 2004 (3)
      • November (2)
      • October (1)

    Get Updates

    Subscribe via RSS
    TwitterFacebookFacebook
  • Other blogs

    • Photography
    • Singularity
    • Hungry
    • Tumblog

    maloki.net

    • Home
    • Projects
    • Contact