In thx there are many helper methods. The convention is that if a class is not meant to be instantiated and its only purpose is to add functionalities to other types than it will be named after the type it enhances but in the plural form. So it is now pretty obvious that Arrays is a helper class for the Array
type as Strings is a helper class for String
.
So far I am really focused to add (and to improve) features and I am not really worry about file size or performances. Some of the helper classes are pretty dense and refers to other dense definitions which can add some weight to your output. This issue will be addressed at some point by improving thx compatibility with haxe DCE (Dead Code Elimination) or maybe splitting the classes.
Back on the post topic, I’d like to introduce a few of the options you can find in Arrays
. So I assume for the following examples that using Arrays;
is always in place since that makes the programmer life even easier.
Arrays
adds some functions to enhance method chaining, for example you can write:
var arr = [].add(1).add(2).addIf(value > 2, 3).remove(2);
Many methods in Arrays
derive from the Lambda
class found in the std
library but tend to be more flexible and the definitions are also repeated in Iterators and Iterables to make your life easier. Whenever is possible inline
is used to prevent adding a new level of indirection. Note that if a method returns a list, it always returns an Array
, even if you are using the Iterator
or Iterable
type. Arrays are pretty fast on all the platforms and are a lot more flexible than the iterators.
// Ints.range(10) returns an array of int from 0 to 9 var arr = Ints.range(10) .filter(function(d) return 0 == d % 2) .map(function(d,i) return d*d); // arr is [0,4,16,36,64]
Since hxculture has been included inside thx, you can format many types (and Array is not an exception) to string in a localized fashion. So far you can use 2 formats with array J
for “join” and C
for “count”.
trace(arr); // [0,4,16,36,64] trace(arr.format("J")); // 0, 4, 16, 36, 64 // the first parameter is for the format of each individual value trace(arr.format("J", ["D"])); // 0.00, 4.00, 16.00, 36.00, 64.00 // the second parameter is for empty array trace([].format("J", ["D", "-"])); // - // the third parameter is the separator trace(arr.format("J", ["D", "-", "; "])); // 0.00; 4.00; 16.00; 36.00; 64.00 // the fourth parameter is the max number of elements to display trace(arr.format("J", ["D", "-", "; ", "3"])); // 0.00; 4.00; 16.00 ... // the fifth parameter is for the remaining elements after max trace(arr.format("J", ["D", "-", "; ", "3", " and more"])); // 0.00; 4.00; 16.00 and more trace(arr.format("C")); // 5
You can also create a format function using var f = Arrays.formatf("J", ["D"]);
and after that f([1,2,3]);
Functions like .all()
/.any()
permits to check if all or at least one of the elements of the array matches a certain rule. The rest of the functions in Arrays
are pretty obvious to catch but if you have any doubt I will be glad to five more insights.