In this post I continue to describe the basic functionalities of thx. I’ve decided to put those classes directly in the root because that make them extremely accessible and cases for name conflicts are really low. Having them in the root is perfect if you don’t want to “waste” a line of code for importing a class that you will use just once. For example I always do stuff like:
var values = Ints.range(10);
If you work a lot with math algorithms, the Ints and Floats classes can really make your life easier. Let’s see some examples:
trace(Ints.range(3)); // -> [0,1,2]
trace(Ints.range(7, 10)); // -> [7,8,9]
trace(Ints.range(0, 10, 2)); // -> [0,2,4,6,8]
trace(Floats.range(0, 10, 2.5)); // -> [0,2.5,5,7.5]
I build parsers, encoders and decoders quite often and if that happen to you too you will probably appreciate Ints.canParse(mystring) and Ints.parse. They are really simple functions but help a lot rationalizing your code. The same functions exist in Floats, Dates and Bools (remember that the helper class has the same name as the class it extends but pluralized). The other said of the coin is Ints.format() which convert the value into its string representation. The format function accept formatting options and optionally the reference to a Culture instance (the default one is used if omitted).
Interpolate is also present in most of the basic types. It is really handy for transitions and you can also specify the equation you want to use (default is a linear equation); you can find more pre-cooked equations in thx.math.Equations). So you can interpolate on Strings, Floats, Values, Dates, Arrays and Dynamics … pretty wide options. Where you find the “interpolate” method, usually you find interpolatef too. It works the same but returns a function that takes a float as argument so that you can reuse the same settings over and over. You may wonder what does it mean that you can interpolate over a String or
Dynamic, but it is easy said. Strings.interpolate extracts the floats/integers values from a pair of strings and interpolate those values. Dynamics.interpolate detects the runtime type of the range values and use the function from the corresponding type; if the values do not match a type with a known interpolator an exception is thrown (instance of thx.error.Error).
One of the function I use a lot for debugging is Dynamics.string() that takes any object and transform it into a nicely formatted string. It is surely heavier than Std.string() but it also tries to give a more complete representation of the value passed. Let’s compare both:
var o = [{
name : "John Doe",
birthdate : Date.fromString("1972-07-05"),
score : 7777.7,
contacts : {
emails : [
{ type : "personal", email : "john@example.com" },
{ type : "work", email : "jd@example.com" }
]
}
}, {
name : "Jane Doe",
birthdate : Date.fromString("1974-06-09"),
score : 77777.7,
contacts : {
emails : [
{ type : "personal", email : "jane@example.com" }
]
}
}];
trace(Std.string(o));
trace(Dynamics.string(o));
The first trace returns (newline added by hand):
[{
name : John Doe,
birthdate : 1972-07-05 00:00:00,
score : 7777.7,
contacts : {
emails : [{
type : <...>, email : <...> },{
type : <...>, email : <...> }] } },{
name : Jane Doe,
birthdate : 1974-06-09 00:00:00,
score : 77777.7,
contacts : {
emails : [{
type : <...>,
email : <...> }] } }]
So you may notice that at some level of recursion the contents are simply striped off; this makes sense in most occasions but I like to have the option to display everything if I have to. This is the output of the second trace (again, newlines added by hand):
[{
name : "John Doe",
birthdate : Wednesday, July 05, 1972,
score : 7,777.70,
contacts : {
emails : [{
type : "personal",
email : "john@example.com"}, {
type : "work", email : "jd@example.com"}]}}, {
name : "Jane Doe",
birthdate : Sunday, June 09, 1974,
score : 77,777.70,
contacts : {
emails : [{
type : "personal",
email : "jane@example.com"}]}}]
So, what are the differences?
- Strings are quoted (this is handy for guessing the type by just looking at the dump).
- Dates and numbers are formatted.
- The whole graph is shown.
That’s all for now