Microsoft Silverlight and Adobe Apollo

I found this post through Reddit and found it particularly interesting. The first point it makes is that Microsoft's new Silverlight browser plugin is more than a Flash killer. Microsoft's apparent strategy is to give developers something more than a simple animation/communication tool--similar to Flex. Silverlight allows the browser to connect with server-side languages such as .NET, Ruby, Python, C#, and Javascript 3.0.

This is all very interesting, but the main point I want to draw attention are the strategies of Microsoft and Adobe. First, Silverlight seems like and interesting product, but Flex has been out since 2004 and if people decide not to go with it then they will probably not go with Silverlight--unless they have a prior investment in Microsoft technologies such as .NET. So here we have Microsoft making a stronger move onto the Internet by allowing people to make real desktop applications online--as opposed to (not fake, but flaky) ones by means of javascript. Second, Apollo is doing the opposite. Using either a normal text editor or the Flex Builder coupled with the Apollo runtime, anyone can create a desktop application with normally web technologies.

As the Web 2.0 move has been to create a richer, more desktop-like and open Internet, we find that Microsoft has found a new way to make this happen. Adobe made this move three years ago and are now trying to encroach on the desktop applications market by making programs easier to code. Is this a follow-the-leader game where Microsoft is yet again catching up and now Adobe is finding more ways to compete with them? What do you think of Silverlight? Further, how do both of these technologies compare to Java Applets?

PHP Closures (and Y Combinator in PHP)

I've updated the closure script and changed the syntax of the inner-lambdas. See this post for the update.

One thing I've always wanted in PHP are closures. I know someone else has already tried to add them in without making a PHP extension, but I decided that I wanted to stay away from PHP's eval. I like the way my solution has turned out except that it requires that get_defined_vars()--or a pseudo args array--be passed in to the first function call instead of automagically figuring that stuff out. Here are some examples how the closures are used...

class Foo
{
	public $bar = 'hi';
}

$foo = new Foo;

$anon = lambda(get_defined_vars(), '', '
	$foo->bar = "hello";
');

$anon->call();

var_dump($foo);

// gives: object(Foo)#1 (1) { ["bar"]=>  string(5) "hello" }

In the previous example, the thing to notice is that object references are preserved throughout the closure. Now here comes a beast... A fixed-point combinator! I modeled this after a javascript example that I found on Douglas Crockford's website. Please realize that for the nested functions I needed to escape the nested single-quotes.

// fixed-point combinator
function Y(Lambda &$le) {
	return lambda(get_defined_vars(), 'Lambda $f', '
		return $f->call($f);
	')->call(lambda(get_defined_vars(), 'Lambda $f', '
		return $le->call(lambda(get_defined_vars(), \'$x\', \'
			return $f->call($f)->call($x);
		\'));
	'));
}

// anonymous factorial function
$factorial =& Y(lambda(array(), 'Lambda $fac', '
	return lambda(get_defined_vars(),\'$n\', \'
		return $n <= 2 ? $n : $n * $fac->call($n - 1);
	\');
'));

echo $factorial->call(5);

// output: 120

As a comparison, it was modeled after:

function Y(le) {
    return function (f) {
        return f(f);
    }(function (f) {
        return le(function (x) {
            return f(f)(x);
        });
    });
}

var factorial = Y(function (fac) {
    return function (n) {
        return n <= 2 ? n : n * fac(n - 1);
    };
});

To use this, you need to take the file and also create a folder that the closures can compile to... Yes! They compile! I do not suggest using this script in production code.. mainly because of its heavy use of a singleton as a stack, but then if anyone wants to benchmark this under a heavy load, I'd be very interested to see the results!

Also, I tried to find a hack around having to pass get_defined_vars() as an argument to the "lambda" (Closure) function, but I obviously did not succeed. Anyway, check out the full source of my php closure script at http://ioreader.com/code/Closure/closure.phps

As a side note, you might be wondering why I simply didn't use PHP's create_function() to create slightly more lambda-style functions. Check out this comment for more info.

EDIT: I renamed the "C" function to "lambda" and removed the ->scope(...) call so that everything is contained within lambda.

Mini App: PHP Hour Tracker

Today I didn't have much to do so I created a system to keep track of my hours. It's a very simple mini application that you'll notice looks uncannily like gmail. I decided to take a break from my normal object-oriented approach to everything and write something without any classes (with the exception of exception handling). There are almost no comments in the script, but it's self-explanatory. Go check out the My Hours Timer at http://ioreader.com/code/timer/. The way it works is that you install it by going to index.php?install and it will attempt to create a SQLite database. I abstracted away the database to functions (without classes!), the trick is a nifty PHP4-era hack that makes singletons by means of declaring a static variable within a function. You can view the source code of My Hours or simply look at how I dealt with database calls...

// OO way with iterators:
$result = $db->query("SELECT ...");
while($result->next())
	$row = $result->current();

// non-OO way
$result = query("SELECT ...");
while(nextResult($result))
	$row = currentResult($result)

I could see this non-OO way being extremely useful where the nextResult and currentResult functions are generic interfaces to different object and data types such as arrays, directory listings, etc. Right now, they remain somewhat ambiguous functions in my mini app by just catering to database results.

flyingwithfire.com changed to ioreader.com

I've changed the URL of my blog so make sure to update your feed readers!

Hello Lisp

As an update to my previous Common Lisp post, I've got Lispbox up and running with Emacs, Allegro CL Express, and SLIME and am slowly working my way through Practical Common Lisp. Sofar I really like where it's going.

I often read on blogs that you either "get" Lisp or you don't. I'm definitely not at that point, but interestingly enough I like it. I like the way that some common things are backward in terms of how they're done in normal programming languages, for example:

C-like: var = 1 + 2 + 3;
Lisp: (setf a (+ 1 2 3))

You might think that that looks ridiculous but it's actually quite neat once you get into some real code. Here's a bit more code from one of the examples that I've been working through in the book.

;; add some cds
(defun add-cds ()
	(loop (add-record (prompt-for-cd))
		(if (not (y-or-n-p "Add another cd? [y/n]: ")) 
			(return))))

I love being a Lisp newb!

Apollo

I've got the Adobe Apollo runtime up and running and have been fooling around with it for a few hours now. What I like about it is that I can make desktop applications with web-based technologies. However, by using Textmate as my editor instead of the Flex one, it seems I am at a disadvantage.

» read the rest of this entry.

Two Weeks of Suck

The past two weeks have been really hectic! I've have to do two papers, an exam, a quiz, and I'm now working on a business case competition. Beside that, I wrote a simple active records scheme in PHP5 that I was planning on writing about at some point. Here's a quick demo of how it works if you don't know what I'm talking about...

	<?php

	error_reporting (E_STRICT);

	define ("PROJECT_DIR", dirname(__FILE__));

	// database
	require PROJECT_DIR .'/database.php';
	require PROJECT_DIR .'/drivers/mysql.php';

	// general functions
	require PROJECT_DIR .'/functions.php';

	// records
	require PROJECT_DIR .'/record.php';
	require PROJECT_DIR .'/recordset.php';

	// finder
	require PROJECT_DIR .'/finder.php';

	// models
	require PROJECT_DIR .'/model.php';
	require PROJECT_DIR .'/table.php';

	/*
	 * test!
	 */

	// connect to the database
	$dba = new DBA_MySQL;
	$dba->connect('localhost', 'db', 'user', 'pass');

	// bring in the model and pass the database connection
	// to it
	$model = new Model($dba);

	// get a model definition and finder for the table 'wt_course_names'

	$finder = $model->getFinder('course_names');

	// find all the rows in this table
	$results = $finder->findAll();

	$i = 1;
	while($results->next())
	{
		// get the current record!
		$course = $results->current();

		// change the name of this course
		$course->name = 'test course '. $i;

		// save it!
		$course->save();

		$i++;
	}

	// free the database result
	$results->free();

	?>
	

Notice how this not only abstracts the database, but also the tables themselves! If you were ever wondering what and how models worked in MVC frameworks, the upcoming article about this code will help you out.

Midterm exams finished!

Holy crap I haven't written anything in days. Well, I was insanely busy studying for midterm exams. Good excuse in my opinion. I'm off tomorrow back to Montreal/Mt. Tremblant.

Otherwise, I was notified about a few bugs in the OneLobby codebase that I released that prevents registration so I will go fix the CAPTCHA, also, I've thought up a few new ideas over the past few weeks that I'll probably be pursuing. Depending on what seat I get on the train(s) home tomorrow, I might be able to work on my laptop for more than an hour or two (my macbookpro battery is fuxed.. AGAIN).

Have a great reading week to anyone reading this from FaceBook.

Happy Valentine’s Day!

Happy Valentine's Day! I just wanted to include a picture from my favorite internet comic, XKCD..

XKCD Valentine's Day

OneLobby

So Geoffrey (my brother) and I worked on OneLobby during the summer and late 2006 but haven't really done anything with it since. The plan was to make it and release it as open-source software anyway, but we never finished it! As I mentioned in a previous blog post, I'm going to release the code. There are a lot of really cool things in it, especially the permissions system, active record implementation, and template engine that geoffrey developed for it. In fact, I think almost all the code is really cool. The framework that was made for it is called FileArts, it's the one I use on all of my projects and is of course developed by Geoffrey. The version that OneLobby uses isn't quite up-to-date, but whatever! Before you get into the code, there are a lot of unfinished things to be done, mainly on the administration side of onelobby. Things that need to be done include:

  • A categories system, I was thinking something like Wikio, such that it would also work together with the tags system.
  • Most of the admin panel
  • Anything that one would expect that needed to be done with cron, eg: mass emails, etc.
  • Some other things I don't remember

If you are wondering wtf OneLobby is anyway, you can check it out over at OneLobby. OneLobby is licenses under the GPL, and if you're planning on doing anything involving its code, I'd really like to know! So, go check out the OneLobby Subversion Repository.