Hello, my name is Joshua Inkenbrandt and I live in Kansas City, Missouri with my wife and two kids. I'm a Mac guy. I'm a Python guy.

My goal is to make cool stuff that's fun and easy to use.

Only showing Posts with tag actionscript X

March 05, 2010

A Completely Unfair Comparison

Many of my colleagues don't understand why I complain as often as I do about ActionScript 3. Sure it's got its quirks and issues, but is it really all that bad? Well, to anyone who has done any significant work with a highly dynamic language like Python or Ruby, it is. I understand that the comparison between a Domain Specific Language like ActionScript 3 and a general purpose language like Python or Ruby isn't really a fair one. That's not the point, though. The point is to see what it would look like to use a more dynamic language (In this case - Python) to accomplish what you would normally have to do in AS3.

In AS3, the Object, Array, String and Date classes all lack any sort of useful API when compared to Python.

Let's start with the Object:

 1 // ActionScript 3
 2 
 3 var object:Object = {key1:'value1', key2:'value2'};
 4 
 5 // Get the keys of the object and store them in an array
 6 var keys:Array = [];
 7 for (var key:String in object)
 8     keys.push(key);
 9 
10 // Get all the values of the object and store them in an array
11 var values:Array = [];
12 for (var key:String in object)
13     values.push(object[key]);
14 
15 // Test to see if the object contains a particular value
16 var containsValue:Boolean = false;
17 for (var key:String in object)
18     if (object[key] == 'value2') containsValue = true;
19 
20 // Add new values to the object
21 var newObject:Object = {key3:'value3'};
22 for (var key:String in newObject)
23     object[key] = newObject[key];
24 
25 // Get the length of the object
26 var objectLength:Number = 0;
27 for (var key:String in object)
28     objectLength++;

Let's do the same in Python:

 1 # Python
 2 
 3 obj = {"key1":"value1", "key2":"value2"}
 4 
 5 # Get all the keys of an object (or dictionary in Python)
 6 keys = obj.keys()
 7 
 8 # Get all the values of a dictionary}
 9 values = obj.values()
10 
11 # Test to see if the dict contains a particular value
12 contains_value = 'value2' in obj.values()
13 
14 # Add new values to the dict
15 obj.update({"key3":"value3"})
16 
17 # Get the length of the object
18 object_length = len(obj)

The lack of built-in support for rudimentary methods really stands out. At least to me.

Then the Array

 1 // ActionScript 3
 2 
 3 var array:Array = ['one', 'two', 'three', 'five'];
 4 
 5 // Looping over values
 6 for each (var value:String in array)
 7     trace(value);
 8 
 9 // Looping over indexes
10 for (var i:Number = 0, l:Number = array.length; i < l; i++)
11     trace('index = ' + i, 'value = ' + array[i]);
12 
13 // Slicing
14 var firstHalf:Array = array.slice(0, 2);
15 var lastHalf:Array = array.slice(2);
16 
17 // Seeing if a value exists in array
18 var valueExists:Boolean = array.indexOf('two') != -1;
19 
20 // Concatenate
21 var newArray:Array = array.concat(['five', 'six']);
22 
23 // Get the number of times a value repeats itself in the array
24 var numValues:Number = 0;
25 for each (var value:String in array)
26     if (value == 'five') numValues++;

And in Python

 1 # Python
 2 # In Python an array is called a list
 3 
 4 array = ['one', 'two', 'three', 'five']
 5 
 6 # Looping over values
 7 for value in array:
 8     print value
 9 
10 # Looping over indexes
11 for i, value in enumerate(array):
12     print "index = %s value = %s" % (i, value)
13 
14 # Slicing
15 first_half = array[:2]
16 last_half = array[2:]
17 
18 # Seeing if a value exists in array
19 value_exists = 'two' in array
20 
21 # Concatenate
22 new_array = array + ['five', 'six']
23 
24 # Get the number of times a value repeats itself in the array
25 num_values = array.count('five');

I don't have much of a beef with the Array in AS3. It's pretty evenly matched when it comes to built in methods compared to Python.

Let's look at the String.

 1 // ActionScript 3
 2 
 3 var string:String = "Hello World";
 4 
 5 // Test to see how may 'l' characters are in the string
 6 var num:Number = 0;
 7 for each (var char:String in string.split(''))
 8     if (char == 'l') num++;
 9 
10 // Capitalize
11 var cap:String = string.substr(0, 1).toUpperCase() + string.substr(1);
12 
13 // Make it title case
14 var title:String = "";
15 var prev:String = " ";
16 for each (var char:String in string) {
17     if (prev == " ") char.toUpperCase();
18     title += char;
19     prev += char;
20 }
21 
22 // See if it starts with "Hell"
23 var startsWith:Boolean = string.substr(0, 3) == "Hell";
24 
25 // See if it ends with "rld"
26 var endsWith:Boolean = string.substr(string.length-4) == "rld";
27 
28 // Remove white space before and after
29 string = "  Hello World  ";
30 var cleanString:String = string.replace(/^\s+/, "").replace(/\s+$/, "");

And in Python we do:

 1 # Python
 2 
 3 string = "Hello World"
 4 
 5 # Test to see how may 'l' characters are in the string
 6 num = string.count("l")
 7 
 8 # Capitalize
 9 cap = string.capitalize()
10 
11 # Make it title case
12 title = string.title()
13 
14 # See if it starts with "Hell"
15 starts_with = string.startswith("Hell")
16 
17 # See if it ends with "rld"
18 ends_with = string.endswith("rld")
19 
20 # Remove white space before and after
21 string = "  Hello World  ";
22 clean_string = string.strip();

Conclusion

These examples express my own laziness, in a way. But it's not because I'm too lazy to write a utility class that simplifies the aforementioned examples. It's because I don't feel like i should have to. You can't blame Flash for following ECMAScript. It's a great scripting language, but what makes ECMAScript great is that it's a prototypical language. What bothers me about ActionScript 3 is that it has moved away from being a prototypical language.

Again, the purpose behind this post isn't to prove the superiority of one language over the other. It's like comparing apples to oranges... I just prefer apples.

February 22, 2010

Late Nights and Gimbal Locks

Here I am and it's 11:25pm... And I'm just getting started.

My nights have recently been filled with overwhelming (I just spelled that right the first time. Yeah me!) amounts of stress. You wanna know why? It's because of a little something called a Gimbal Lock. You see, I didn't take trigonometry (You're not going to believe me, but I just spelled that one right the first time, too!) or calculus. And while I feel like I'm generally pretty decent at algebra and problem solving; calculating points in 3D space and using Quaternions to do so, are not my strong points.

So here I am and it's 11:36pm... And I just wasted 11 minutes.

Update: Finally figured it out... And it's only 2:30am.