I Love My Dynamic Sandboxes
One of the neat tricks of dynamic languages like Python, Ruby, Javascript or even PHP is that you can try any snippet of code in your own little sandbox before you actually use it. This is great for exploring APIs, for learning purposes or for small proofs of concepts.
Let’s take a very simple example. Let’s say you come from a .NET background and are very found of LINQ. You would like to know how this expression would translate in your dynamic language of choice. Here’s a way to do it.
LINQ expression
from name in names
where name.Length > 3
orderby name
select name.ToUpper()
Python
Assuming you are runing on a Unix machine simply type python in a terminal. Once in the interactive interpreter, you are free to do pretty much anything you want with the language. You can import any eggs you have installed and anything that is in your PYTHONPATH. You can even add paths to your PYTHONPATH on the fly if you want to. At the prompt you would simply type the commands you want until you get the desired result.
Here’s a way to translate the above expression in the interactive interpreter :
>>> names = [ "guy","jean","bob","serge","jacques","yvon"]
>>> sorted(name.upper() for name in names if len(name) >3)
['JACQUES', 'JEAN', 'SERGE', 'YVON']
Pretty concise expression wouldn’t you say? The last line is python outputting you the result of you query. Neat huh ?
RUBY
Ruby had it’s own interpreter. It’s called IRB. On most UNIX machines, you will need to install it, unlike python. It pretty much works the same way as the Python interactive interpreter.
You start it by calling IRB. Then you have access to pretty much anything you’d like just like in Python.
The console looks like this:
irb>names = [ "guy","jean","bob","serge","jacques","yvon"]
irb> names.select {|name| name.length() >3}.each{ |name| name.upcase}.sort
=> ["JACQUES", "JEAN", "SERGE", "YVON"]
IRB also kindly print out the results.
By the way, all the examples above are built into the language without the need of anything like LINQ.
Javascript/jQuery
As far as Javascript in concerned, the way to have some fun is to use the FireFox add-on called FireBug.
One there, simply go into the console section and any Javascript library that is available in the page you are currently viewing will be accessible. For this example, we playing with the jQuery library by going directly on the jQuery.com site. Isn’t that sweet. Wanna resolve the above LINQ query using FireBug as your sandbox ? After some fiddling here is the solution I came up with :
var names = [ "guy","jean","bob","serge","jacques","yvon"];
jQuery.grep(names, function(name){return name.length > 3})
.map(function(name,i){return name.toUpperCase()})
.sort()
FireBug kindly prints out the result of my execution as this :
["JACQUES", "JEAN", "SERGE", "YVON"]
PHP
How bout some PHP ? PHP comes with it’s own interpreter. Simply type php -a in a console. It’s not as cool as Python’s or Ruby’s but it does a decent enough job. So how does this expression translate in PHP ? Well, it’s definitely more verbose, but at least you got to try it out in a sandbox first to minimize any surprises from the quirks in the language.
php > function toUpper($name){return strtoupper($name);}
php > function longerThanThree($name){return strlen($name)>3;}
php > $names = array(“guy”,”jean”,”bob”,”serge”,”jacques”,”yvon”);
php > $filteredNames = array_map(“toUpper”,array_filter($names,”longerThanThree”));
php > sort($filteredNames);
php > print_r($filteredNames);
Array
(
[0] => JACQUES
[1] => JEAN
[2] => SERGE
[3] => YVON
)
Conclusion
There you go. Mission accomplished. Had you done the exercise yourself, you just would of quickly learned how to write a snippet of code in 5 different languages !
I think this is all pretty neat. As an added bonus, besides the PHP example, all solutions are very elegant and were discovered using trial and error as a learning tool. Something that is unfortunately not available in non-dynamic languages.
-Nicholas Lemay



