Google App Engine

I worked my way through the Google App Engine tutorial this evening - pretty cool stuff.  Seems like a great way to get an app up and running if your language of choice is python.  Python seems like a great language - very powerful.  And there are a number of web application frameworks you can use - like django.

The tutorial takes you through the process of creating a simple guestbook application.  Here is mine.

Haskell B Curry

This dude - a famous logician who I mentioned in my last post - was quite a prolific guy.  Or at least he was adept at getting stuff named for him:
I found most of this stuff while researching my last blog post - pretty wild.

Schonfinkelization

Yesterday, I posted on anonymous methods, the syntax to create them and how to use them to create a simple closure.  In a previous post, I used an anonymous method to form a closure for the purposes of caching a result.  This type of use is called memoization.  Today, I'd like to show a different type of use for a closure called Currying.

"Currying" or "Schonfinkelization", named for Haskell B Curry or Moses Schonfinkel respectively, is a programming technique wherein a function is created which takes a function with multiple arguments and returns a function which will only take a single argument - the rest of the arguments having been bound to the returned function.

Interestingly, the idea was originally conceived by Schonfinkel but Curry's name is the one usually attached to the concept - it's rarely called "Schonfinkelization",  Just do a Google search on the two terms to see for yourself.

Here is an example of using an anonymous method to create a currying function:



As you can see, this generic Curry function takes a delegate type of Func<T, T2, TResult> - this is a function pointer for any function which takes two arguments and returns a value.  The Curry function also takes a value which will be bound to the returned function.  It returns a delegate type of Func<T, TResult> which represents any function which takes a single argument and returns a value. 

Here is a test where we leverage the Curry function:



In this test, we use the Curry function to bind the Add function of the AdditionTheHardWay class to a variable - in this case the integer 90.  This gives us a new function which is represented by the delegate addToX.  We can later invoke this function pointer and provide it with a single argument to do our addition.

C# Delegates

My friend Mark has a new blog with some great .NET tips.  He's been posting daily and it's been really good so far.  Lots of good tips on C# language features for both .NET 3.0 and 2.0.

Mark and I presented some of these language features last week for our team at work and one of the things we tallked a bit about was anonymous methods and some of the new delegate features.  These features of C# are becoming increasingly important for developers to understand.  I'd like to present this code to you today.

For the examples I implemented a class which (for kicks) does recursive addition - here is that class:

AdditionTheHardWay

Using the "AdditionTheHardWay" class we can show a few different ways of using delegates with simple instance methods.

In the traditional way, available since .NET 1.0, you would first declare the delegate type with the type parameters you needed like so:

public delegate int AdditionDelegate(int x, int y)

Once you had declared the delegate type you were free to use it with methods which had the same signature.  Here is an example:

Traditional Delegate Test Method

In the above test, we create an instance of the AdditionTheHardWay class and then assign the Add method to the AdditionDelegate.  We can then invoke the delegate or function pointer at a later time or even add more methods to the delegate (the delegates in C# are multi-cast).

With .NET 2.0, Microsoft introduced a number of changes to the C# language which had a profound impact on how delegates could be used - the first of these is generics.  Generics allow us to use type arguments for the delegate types.  Here is a delegate declaration using generics:

genericdelegate

With this new generic delegate declaration we are free to assign methods which do not accept integer arguments.  In this case any method which takes two parameters and returns some value will match our generic delegate.

The second major feature introduced with .NET 2.0 was delegate inference.  With delegate inference we no longer need to specify the type of our delegate when assigning it.  Here is another example:

simplifieddelegate

In this test we use the GenericDelegate type for our delegate with integer type arguments but when we assign the Add method we don't need to specify the delegate type again.  The complier is able to infer the type which allows us to shorten and simplify the syntax a bit.

The final feature introduced with .NET 2.0 which is relevant here is anonymous methods.  With an anonymous method you can create your delegate or function pointer inline.  Here is an example:


There are a few things going on in the example above which may not be obvious.  First, the anonymous method syntax requires the use of the delegate keyword.  After the delegate keyword, specify any parameters that the anonymous method will take and then, within the curly braces, you write the method itself.  Pretty straight forward, right?

In this case we are assigning the anonymous method to a delegate of type Func<int, int>.  This is a built-in generic delegate type which shipped with .NET 3.0 along with a number of other built-in generic delegate types (a subject for another post).  For now, know that this is the same as any method which will take an argument and return a value.

Finally, of note in the above example, is that in creating an anonymous method which takes a parameter we are actually creating a closure - which is simply a variable bound to a method.  This is a very powerful technique which is made possible through the use of anonymous methods.

Project Euler (Problem III)

I did a little analysis of my blog posts from the past month that I've been blogging and it turns out that the Project Euler related content is the most popular and most viewed content on the site. Who would've thought?  I guess the internet is a popular place for mathturbation.

The third Project Euler problem is a prime factorization problem.  There are many, many, many problems on Project Euler which require factoring prime numbers and this is one of the easiest.  Given a very large number we need to find the largest prime factor to submit as our answer.  The solution we use for prime factorization we will be able to reuse later on in a number of other problems.

There are actually many different methods for finding prime factors and it can get complicated quickly.  Check out the various "sieves" and other methods from wikipedia - GNFS, SNFS, Euler, Fermat.  Did you see the Big O notation for the general number field sieve?  I don't know how to parse that - Crazy!

I used trial division.  It is the simplest method of integer factorization.  It's not going to win any RSA contests but it'll work for the Project Euler problems.  Here is the basic trial division algorithm (in C#):

show/hide

Now you just plug in your big number and voila - you've solved the problem. It runs suprisingly fast on my box.

Cleveland Day of .Net

It looks like Cleveland is going to be having a day of .NET this Spring. 

What is a day of .NET?  It's a chance for area developers to get together, learn from each other and network.  It's also free

You can get details on the event as well as register here (there are only 250 spots open).

The Cleveland day of .NET is May 17th (a Saturday) in a location yet to be determined (but assuredly somewhere local).

Quick HQ9+ Interpreter

Because I'm on this esoteric languages kick and for some quick stupid fun I threw together this HQ9+ interpreter.  Enjoy.

Vista Upgrade

I underwent the upgrade to Vista Ultimate this past week courtesy of Microsoft and, although I'm not blown away, I'm definitely satisfied with the new OS. 

The highly-touted aero interface features are nice (obligitory screenshot):

aerodesktop

As expected the UAC (or user access controls) are a bit of a nuisance but it's configurable.

Performance-wise it has run without issue and is consuming about 48% of my available 2GB RAM when running VS2008 standard, firefox and a couple other apps (my standard dev-mode).  It runs in the low to mid 30% range when it's just the desktop.  Not very specific benchmarks - I know.  Maybe I'll take the time to do something a little more detailed at some point.

Would I have upgraded if I had to pay for it?  No. 

Is it worth the $270 it costs on Amazon?  Probably not. 

Am I glad that I can run it free?  Definitely yes. 

While I wouldn't say that the OS is a step-back from XP; it's definitely not a great-leap forward.  If Microsoft really wants people to upgrade to the new OS they really need to either add some features which make the switch compelling (aero et al. isn't enough) or come way down on price.

Slickrun

One of the most important things you can do to increase your productivity whether coding or documenting or computing in some other form is just stick to the keyboard. 

Don't move your hands.  Just get into a comfortable, ergonomic position and type, and use keyboard shortcuts.  It really isn't that hard once you get used to it. 

Windows power users have long known that one of the key tools is the run window ('windows key + R') which lets you launch any number of applications right from your keyboard. 

Quick Windows tip: update your path environment variable to include the directories of apps and utilities you often use so you can use the run window to launch them.  For years, I've put most of mine in C:\utils and added that to the path.

But now I extend that solution with Slickrun from bayden systems.  Slickrun is essentially the run window on steroids.  It is a "free floating command line utility"  which you can extend by setting up "magic words" or command aliases.  Once installed you just launch it with 'windows key + Q' and type the command.  The command line has history and even sports an intellisense-like feature that will find your magic word as you type - it's beautiful.

Technically, some of these features are achievable through the command line or run window if you want to take the time to write all the scripts but with slickrun
the UI is simple to use.  You can also specify a number of different parameters to your custom commands. And command libraries can be exported or imported so you can take it with you or share it with friends.

I hope you start to use this handy tool and find it as helpful as I do.

Esoteric Languages

A while back I wrote about HQ9+ which prints out simple strings and does simple loops as an example of an esoteric language.    But HQ9+ is just the tip of the iceberg, there is a whole wiki full of strange and occasionally funny languages.  Here are a few:
  • Whitespace:  In the whitespace language the only characters which matter are the ones you don't see: the tab, the space and the newline.  The code is invisible and it's unrecoverable if you print it out!  There are whitespace interpreters written in Haskell, Ruby and whitespace.
  • Brainf**k: Which, perhaps because of the provocative name, has dozens of offshoots like smallf**k, dumbf**k and doublef**k.  Brainf**k has a terse syntax but is turing-complete so you can do anything you need to if you can just figure out how.  One of the best variants is:
  • Ook:  Ook is a brainf**k variant but the syntax is transformed to "Orangutan".  Here is a table containing the instructions sets for Ook and brainf**k (from the esolang wiki):
Brainf**k Ook! Description
> Ook. Ook? Move the pointer to the right
< Ook? Ook. Move the pointer to the left
+ Ook. Ook. Increment the memory cell under the pointer
- Ook! Ook! Decrement the memory cell under the pointer
. Ook! Ook. Output the character signified by the cell at the pointer
, Ook. Ook! Input a character and store it in the cell at the pointer
[ Ook! Ook? Jump past the matching ] if the cell under the pointer is 0
] Ook? Ook! Jump back to the matching [

As turing-complete languages, whitespace, brainf**k and it's variants are as powerful as any OO, functional or other modern languages.  Turing-completeness refers to a languages computational ability - essentially a turing-complete language can run all computable functions or algorithms.  Contrast these with HQ9+ which has very very limited ability to compute anything.

That said, these languages are also examples of Turing tar-pits.  Although you can do anything you might need to - you first have to figure out how to do it.  And with languages as sparse as these that is not easy.

Happy Pi Day!

Today is Pi day!  My favorite pi related cartoon (from xckd):
HelpImStuckInaUniverseFactory

 

Heroes happen {here}

I attended the Cleveland Ohio "Heroes Happen" Microsoft event this afternoon.  And despite all the goofy marketing stuff it was a great event.

While a little depth on some key topics would have been ideal, I knew going in that this was more of an introduction and marketing affair so I wasn't disappointed.  And as an introduction to many of the new features and technologies available in the latest suite of Microsoft products it was a very successful and well-attended event.  There was easily a couple hundred developers in the sessions I attended.

The Session List:
  • Next Generation Web Technology - LINQ, AJAX this session covered technologies which are somewhat new and have buzzy acronyms.  But, unfortunately, no ASP.NET MVC - apparently too new.  I've been using the VS2008 trial version for 80 some days so I've seen most of the new features and used some.
  • Creating Office Apps - A session on leveraging new framework features to extend office functionality and develop office applications.  I probably won't ever use this - but it was interesting.
  • Connected Applications - This was an interesting session covering WCF (which I should learn more about) and SmartClient deployment as well as a few other topics.
Obviously - for me - the most interesting session was the first one which focused on web and .NET more generally but I didn't mind sitting through the rest for the free software.  And there was a lot of free software!

The Software List:
  • Windows Server 2008 (evaluation license)
  • Visual Studio 2008 Standard Edition (Fully licensed!!!)
  • SQL Server 2008 CTP (evaluation license)
  • Windows Moblie Developer kit
  • Windows Vista Ultimate (apparently fully licensed)
  • A Lunch box (That's not software? File it under other SWAG)
Not a bad haul really.  OK, so some of this isn't licensed fully and I may never use it.  But the VS2008 and the Vista Ultimate I probably will - and those together retail for a couple hundred bucks.  Thanks Microsoft!

One Reason For This Blog

One reason I started blogging was to keep track of stuff that I'm learning/researching/reading-about in journal form so that I can go back to it and see it in my own words at a later date.  It also turns out that most people learn concepts/facts/other-things more deeply, more thoroughly and so-on when they write them down.  That's another thing I'm trying to do here.
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011