"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...
"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...
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.
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:
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:
For a better explanation, I suggest reading Kalid Azad’s article and the paper by Charles McEniry.
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.
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.
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:
Uninstallation:
Just a summary on the project:
Motivations
Features
Known Bugs:
Future of the Project:
If you find any bugs, have comments/suggestions, please leave a comment or contact me using the Contact link. Thanks.
#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