I've been tinkering around with C# and Flex a lot this year, but I haven't been posting much.
So in the interest of posting *something* I give you...
*drumroll*
THE WAR OF THE ROOSEVELTS!
You, standing in the (uneven?) shoes of Franklin D. Roosevelt, finally get a posthumous chance to stand up to your bullying older cousin Teddy in a game of WAR...well, at least the game of war as I knew it growing up. Wikipedia's got it's own ideas.
I lost interest in it by the time I got to the gui, so that aspects (aka as the part people actually see) is a bit...rough. Just a bit. ENJOY!
Here's the source, but I won't waste my time looking up the svn link.
I ran into a problem the other day where I needed a way to temporarily store data between page requests. Typically I'm able to stash this sort of thing in the session scope, but these requests originated from different sources and I prefer to avoid (de)serializing when I can.
Instead I set up something akin to one of those pay-per-use lockers. You stick your items in the bin, drop in a few quarters, and take the newly unlocked key. Later you come back and use that key to retrieve your items. Your key is now 'locked' back into the starting position and the cycle begins anew.
Like one of those pay-per-use lockers you just stash your data, save they key, use the key, trash the data.
Simple as pie.
This isn't the sort of thing that comes up often, but should it arise I've got just the tool for the job!
Example Usage:
// initialize the locker
application.cacheLocker = CreateObject("component","cacheLocker").init();
// store some arbitrary data
key = application.cacheLocker.store([1,2,3,4]);
// then retrieve the data using the saved key
// throws a CacheException if the key doesn't 'fit'
arbitraryNumbers = application.cacheLocker.retrieve(key);
The data is destroyed after being retrieved; it's a one time only locker.
There are two important things to keep in mind when using this utility:
There's currently no mechanism for cleaning out lockers, so if you're not regularly retrieving your data then this thing is just going to grow, and grow, and grow.
The locker is not stored in any sort of persistent memory. If ColdFusion goes down, then the lockers are destroyed.
It's a cool site that lets you pass in a width and a height to generate an image which could come in really handy for quickly mocking up web pages.
I can't believe I never thought of doing something like this!
ColdFusion is a great language for doing this sort of thing, so it was trivial to whip something up quickly.
I was originally writing a file to disk because I didn't realize you could stream an image variable to the browser, but my other co-worker Jim came to the rescue with the cfimage "writeToBrowser" action!
<cfset params = listToArray(cgi.query_string,"x") />
<cfif arrayLen(params) lt 2>
<cfthrow message="Input should be like ?[w]x[h]"/>
</cfif>
<cfset width = params[1] />
<cfset height = params[2] />
<cfset color = "gray" />
<cfif not (isNumeric(width) and isNumeric(height))/>
<cfthrow message="Width/Height should be numeric ?100x100">
</cfif>
<cfset image = ImageNew("", width, height,"rgb", color) />
<cfset ImageDrawText(image, "#width# x #height#", 0, 10)>
<cfimage source="#image#" action="writeToBrowser"/>
This year I've decided to really get into C#. My .NET experience is shall at best so aiming to rectify, I picked up C# in Depth and commenced skimming!
Now, I've made my fair share of M$ snide asides, but I'm having a hard time coming to gripes with C#. Everything I run into either "just works" or exceeds my expectations. And the cool features are in fact, quite cool! Color me impressed!
Noob!
For fun I rewrote a few of my Project Euler Solutions to buff up on the syntax. After I got the semi-colons and brackets all figured out, I moved on to something a little bigger.
I wanted a simple program to run and benchmark my solutions, so I wouldn't have to do as much leg work every time I converted a problem. I figured this would be a simple enough thing to do, and it would provide a good foundation for a future gui application and beginning unit testing.
Generics, Delegates and Lambdas
Generic Collections provide a data structure that I can access and use just like an array, but also provides methods for dealing with delegates.
Delegates are very similar to closures, blocks, procs, and lambdas like I've worked with in other languages, so the transition was smooth. The lambda syntax was particularly reminiscent of pythonic list comprehensions.
Thanks to delegates, I can turn this:
var matchingTypes = new List<Type>();
foreach(t in CurrentTypes) {
if(t.IsSubclassOf(parentType) {
matchingTypes.Add(t);
}
}
return matchingTypes;
return CurrentTypes.FindAll(
t => t.IsSubclassOf(parentType)
);
Not too shabby, eh?
Reflection
Most of my programming has been in ColdFusion, JavaScript and Ruby. There's been a little bit of this and a little bit of that peppered in there, particually C and Java while I was at UCF, but for the most part I've enjoyed working with dynamic and/or interpreted languages. Meta-programming is common in these types of languages, but I was surprised and impressed to read up on reflection. In this case, reflection allows me to dynamically detect and run my problems, which makes it easier (and cleaner) for me to add new solutions.
Here's a simplified "ClassMaster" class I use to wrap my reflection calls for listing and creating classes, so you can see what I'm on about:
class ClassMaster
{
private Assembly CurrentAssembly { get; set; }
private List<Type> CurrentTypes { get; set; }
public ClassMaster()
{
CurrentAssembly = Assembly.GetExecutingAssembly();
CurrentTypes = new List<Type>(CurrentAssembly.GetTypes());
}
// should probably take arguments to pass thru...somehow
public Object CreateClass(Type classType)
{
return CurrentAssembly.CreateInstance(classType.FullName);
}
public List<Type> getTypesByParentClass(Type parentType)
{
return CurrentTypes.FindAll(
t => t.IsSubclassOf(parentType)
);
}
}
That's it for now. I'll be looking into LINQ and unit testing in the next couple weeks, and then I'm on to the gui. ASP, SilverLight, and WPF here I come!