class thx.Arrays
Available on all platforms
Arrays
provides additional extension methods on top of the Array
type.
Note that some of the examples imply using thx.Arrays;
.
Class Fields
static inline function after<T>(array:ReadonlyArray<T>, element:T):Array<T>
Finds the first occurrance of element
and returns all the elements after it.
static function all<T>(arr:ReadonlyArray<T>, predicate:T -> Bool):Bool
Checks if predicate
returns true for all elements in the array.
static function any<T>(arr:ReadonlyArray<T>, predicate:T -> Bool):Bool
Checks if predicate
returns true for at least one element in the array.
static function append<T>(array:Array<T>, element:T):Array<T>
Arrays.add pushes an element at the end of the array
and returns it. Practical
for chaining push operations.
static function appendIf<T>(array:Array<T>, cond:Bool, element:T):Array<T>
Arrays.addIf conditionaly pushes an element at the end of the array
and returns it.
Practical for chaining push operations.
static function at<T>(arr:ReadonlyArray<T>, indexes:ReadonlyArray<Int>):Array<T>
Creates an array of elements from the specified indexes.
static inline function before<T>(array:ReadonlyArray<T>, element:T):Array<T>
Finds the first occurrance of element
and returns all the elements before it.
static function commonsFromStart<T>(self:ReadonlyArray<T>, other:ReadonlyArray<T>, ?equality:T -> T -> Bool):Array<T>
Traverse both arrays from the beginning and collect the elements that are the same. It stops as soon as the arrays differ.
static function compact<T>(arr:ReadonlyArray<Null<T>>):Array<T>
Filters out all null elements in the array
static function compare<T>(a:ReadonlyArray<T>, b:ReadonlyArray<T>):Int
Compares two arrays returning a negative integer, zero or a positive integer.
The first comparison is made on the array length.
If they match each pair of elements is compared using thx.Dynamics.compare
.
static function contains<T>(array:ReadonlyArray<T>, element:T, ?eq:T -> T -> Bool):Bool
Returns true
if element
is found in the array.
An optional equality function can be passed as the last argument. If not provided, strict equality is adopted.
static function containsAll<T>(array:Array<T>, elements:Iterable<T>, ?eq:T -> T -> Bool):Bool
Returns true
if all elements in elements
are found in the array.
An optional equality function can be passed as the last argument. If not provided, strict equality is adopted.
static function containsAny<T>(array:ReadonlyArray<T>, elements:Iterable<T>, ?eq:T -> T -> Bool):Bool
Returns true
if any element in elements
is found in the array.
An optional equality function can be passed as the last argument. If not provided, strict equality is adopted.
static function count<T>(arr:ReadonlyArray<T>):Map<T, Int>
Returns a Map containing the number of occurrances for each value in the array.
static function create<T>(length:Int, fillWith:T):Array<T>
Creates a new Array
with length
elements all set to fillWith
.
static function cross<T>(a:ReadonlyArray<T>, b:ReadonlyArray<T>):Array<Array<T>>
It returns the cross product between two arrays.
var r = [1,2,3].cross([4,5,6]);
trace(r); // [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]
static function crossMulti<T>(array:ReadonlyArray<ReadonlyArray<T>>):Array<Array<T>>
It produces the cross product of each array element.
var r = [[1,2],[3,4],[5,6]].crossMulti();
trace(r); // [[1,3,5],[2,3,5],[1,4,5],[2,4,5],[1,3,6],[2,3,6],[1,4,6],[2,4,6]]
static function distinct<T>(array:ReadonlyArray<T>, ?predicate:T -> T -> Bool):Array<T>
Returns a new array containing only unique values from the input array. Input array does not need to be sorted. A predicate comparison function can be provided for comparing values. Default comparison is ==.
static function each<T>(arr:ReadonlyArray<T>, effect:T -> Void):Void
Applies a side-effect function to all elements in the array.
static function eachPair<TIn, TOut>(array:ReadonlyArray<TIn>, callback:TIn -> TIn -> Bool):Void
It allows to iterate an array pairing each element with every other element in the array.
The iteration ends as soon as the callback
returns false
.
static function eachi<T>(arr:ReadonlyArray<T>, effect:T -> Int -> Void):Void
Applies a side-effect function to all elements in the array.
static function equals<T>(a:ReadonlyArray<T>, b:ReadonlyArray<T>, ?equality:T -> T -> Bool):Bool
It compares the lengths and elements of two given arrays and returns true
if they match.
An optional equality function can be passed as the last argument. If not provided, strict equality is adopted.
static function filterNull<T>(a:ReadonlyArray<Null<T>>):Array<T>
Filters out all null
values from an array.
static function find<T>(array:ReadonlyArray<T>, predicate:T -> Bool):Null<T>
It returns the first element of the array that matches the predicate function. If none is found it returns null.
static function findIndex<T>(array:ReadonlyArray<T>, predicate:T -> Bool):Int
It returns the index of the first element of the array that matches the predicate function.
If none is found it returns -1
.
static function findLast<T>(array:ReadonlyArray<T>, predicate:T -> Bool):Null<T>
It returns the last element of the array that matches the provided predicate function. If none is found it returns null.
static inline function first<T>(array:ReadonlyArray<T>):Null<T>
It returns the first element of the array or null if the array is empty.
static inline function flatMap<TIn, TOut>(array:ReadonlyArray<TIn>, callback:TIn -> Array<TOut>):Array<TOut>
It traverses an array of elements. Each element is split using the callback
function and a 'flattened' array is returned.
var chars = ['Hello', 'World'].flatMap(function(s) return s.split(''));
trace(chars); // ['H','e','l','l','o','W','o','r','l','d']
static function flatten<T>(array:ReadonlyArray<Array<T>>):Array<T>
It takes an array of arrays and 'flattens' it into an array.
var arr = [[1,2,3],[4,5,6],[7,8,9]];
trace(arr); // [1,2,3,4,5,6,7,8,9]
static inline function from<T>(array:ReadonlyArray<T>, element:T):Array<T>
Finds the first occurrance of element
and returns all the elements from that point on.
static function groupBy<TKey, TValue>(arr:ReadonlyArray<TValue>, resolver:TValue -> TKey):Map<TKey, Array<TValue>>
Returns a Map of arrays. Each value in the array is passed to resolver
that returns a key to use
to group such element.
This method is tagged with @:generic
and needs a compatible type to be used (ex: no anonymous objects).
In case you have to use a type that is not supported by @:generic
, please use groupByAppend
.
static function groupByAppend<TKey, TValue>(arr:ReadonlyArray<TValue>, resolver:TValue -> TKey, map:Map<TKey, Array<TValue>>):Map<TKey, Array<TValue>>
Available on cpp, java, js, neko, php, python, swf
Each value in the array is passed to resolver
that returns a key to use to group such element.
Groups are appended to the passed map.
static function groupByIndex<A, K>(arr:ReadonlyArray<A>, groupKey:Int -> K):Map<K, Array<A>>
Group the array by a function of the index.
static inline function hasElements<T>(array:ReadonlyArray<T>):Bool
Returns true
if the array contains at least one element.
static inline function head<T>(array:ReadonlyArray<T>):Null<T>
It returns the first element of the array or null if the array is empty. Same as first
.
static inline function initial<T>(array:ReadonlyArray<T>):Array<T>
Get all the elements from array
except for the last one.
static inline function isEmpty<T>(array:ReadonlyArray<T>):Bool
It returns true
if the array contains zero elements.
static inline function last<T>(array:ReadonlyArray<T>):Null<T>
It returns the last element of the array or null if the array is empty.
static function mapRight<TIn, TOut>(array:ReadonlyArray<TIn>, callback:TIn -> TOut):Array<TOut>
Same as Array.map
but traverses the array from the last to the first element.
static function mapi<TIn, TOut>(array:ReadonlyArray<TIn>, callback:TIn -> Int -> TOut):Array<TOut>
Same as Array.map
but it adds a second argument to the callback
function with the current index value.
static function order<T>(array:ReadonlyArray<T>, sort:T -> T -> Int):Array<T>
It works the same as Array.sort()
but doesn't change the original array and returns a sorted copy it.
static function pull<T>(array:Array<T>, toRemove:ReadonlyArray<T>, ?equality:T -> T -> Bool):Void
Pulls from array
all occurrences of all the elements in toRemove
. Optionally takes
an equality
function.
static function pushIf<T>(array:Array<T>, condition:Bool, value:T):Array<T>
It pushes value
onto the array if condition
is true. Also returns the array for easy method chaining.
static function reduce<TElement, TAcc>(array:ReadonlyArray<TElement>, callback:TAcc -> TElement -> TAcc, initial:TAcc):TAcc
It applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value.
static inline function reduceRight<TElement, TAcc>(array:ReadonlyArray<TElement>, callback:TAcc -> TElement -> TAcc, initial:TAcc):TAcc
Same as Arrays.reduce
but starting from the last element and traversing to the first
static inline function reducei<TElement, TAcc>(array:ReadonlyArray<TElement>, callback:TAcc -> TElement -> Int -> TAcc, initial:TAcc):TAcc
It is the same as reduce
but with the extra integer index
parameter.
static function removeAll<T>(array:Array<T>, element:T, ?equality:T -> T -> Bool):Void
Remove every occurrance of element
from array
. If equality
is not specified, strict equality
will be adopted.
static function resize<T>(array:Array<T>, length:Int, fill:T):Array<T>
Resizes an array of T
to an arbitrary length by adding more elements to its end
or by removing extra elements.
Note that the function changes the passed array and doesn't create a copy.
static inline function rest<T>(array:ReadonlyArray<T>):Array<T>
Returns all but the first element of the array
static inline function reversed<T>(array:ReadonlyArray<T>):Array<T>
Creates a copy of the array with its elements in reverse order.
static function rotate<T>(arr:ReadonlyArray<ReadonlyArray<T>>):Array<Array<T>>
Transforms an array like [[a0,b0],[a1,b1],[a2,b2]]
into
[[a0,a1,a2],[b0,b1,b2]]
.
static inline function sample<T>(array:ReadonlyArray<T>, n:Int):Array<T>
Returns n
elements at random from the array. Elements will not be repeated.
static inline function sampleOne<T>(array:ReadonlyArray<T>):Null<T>
Returns one element at random from the array or null if the array is empty.
static function shuffle<T>(a:ReadonlyArray<T>):Array<T>
It returns a copy of the array with its elements randomly changed in position.
static function split<T>(array:ReadonlyArray<T>, parts:Int):Array<Array<T>>
Splits an array into a specified number of parts
.
static function splitBy<T>(array:ReadonlyArray<T>, len:Int):Array<Array<T>>
Splits an array into smaller arrays at most of length equal to len
.
static function splitByPad<T>(arr:Array<T>, len:Int, pad:T):Array<Array<T>>
Splits an array by the given number and pads last group with the given element if necessary.
static inline function take<T>(arr:ReadonlyArray<T>, n:Int):Array<T>
Returns the first n
elements from the array.
static inline function takeLast<T>(arr:ReadonlyArray<T>, n:Int):Array<T>
Returns the last n
elements from the array.
static function traverseOption<T, U>(arr:ReadonlyArray<T>, f:T -> Option<U>):Option<Array<U>>
Traverse the array with a function that may return values wrapped in Option. If any of the values are None, return None, otherwise return the array of mapped values in a Some.
static function traverseValidation<E, T, U>(arr:ReadonlyArray<T>, f:T -> Validation<E, U>, s:Semigroup<E>):Validation<E, Array<U>>
Traverse the array with a function that may return values wrapped in Validation. If any of the values are Failures, return a Failure that accumulates all errors from the failed values, otherwise return the array of mapped values in a Success.
static function unzip<T1, T2>(array:ReadonlyArray<Tuple2<T1, T2>>):Tuple2<Array<T1>, Array<T2>>
Unzip an array of Tuple2
static function unzip3<T1, T2, T3>(array:ReadonlyArray<Tuple3<T1, T2, T3>>):Tuple3<Array<T1>, Array<T2>, Array<T3>>
Unzip an array of Tuple3
static function unzip4<T1, T2, T3, T4>(array:ReadonlyArray<Tuple4<T1, T2, T3, T4>>):Tuple4<Array<T1>, Array<T2>, Array<T3>, Array<T4>>
Unzip an array of Tuple4
static function unzip5<T1, T2, T3, T4, T5>(array:ReadonlyArray<Tuple5<T1, T2, T3, T4, T5>>):Tuple5<Array<T1>, Array<T2>, Array<T3>, Array<T4>, Array<T5>>
Unzip an array of Tuple5
static inline function with<T>(arr:ReadonlyArray<T>, el:T):ReadonlyArray<T>
Returns a copy of the array with the new element added to the end.
static function withInsert<T>(arr:ReadonlyArray<T>, el:T, pos:Int):ReadonlyArray<T>
Returns a copy of the array with the new element inserted at position pos
.
static inline function withPrepend<T>(arr:ReadonlyArray<T>, el:T):ReadonlyArray<T>
Returns a copy of the array with the new element added to the beginning.
static function withSlice<T>(arr:ReadonlyArray<T>, other:ReadonlyArray<T>, start:Int, length:Int = 0):ReadonlyArray<T>
Returns a copy of the array with the other
elements inserted at start
. The length
elements after start
are going to be removed.
static function zip<T1, T2>(array1:ReadonlyArray<T1>, array2:ReadonlyArray<T2>):Array<Tuple2<T1, T2>>
Pairs the elements of two arrays in an array of Tuple2
.
static function zip3<T1, T2, T3>(array1:ReadonlyArray<T1>, array2:ReadonlyArray<T2>, array3:ReadonlyArray<T3>):Array<Tuple3<T1, T2, T3>>
Pairs the elements of three arrays in an array of Tuple3
.
static function zip4<T1, T2, T3, T4>(array1:ReadonlyArray<T1>, array2:ReadonlyArray<T2>, array3:ReadonlyArray<T3>, array4:ReadonlyArray<T4>):Array<Tuple4<T1, T2, T3, T4>>
Pairs the elements of four arrays in an array of Tuple4
.
static function zip5<T1, T2, T3, T4, T5>(array1:ReadonlyArray<T1>, array2:ReadonlyArray<T2>, array3:ReadonlyArray<T3>, array4:ReadonlyArray<T4>, array5:ReadonlyArray<T5>):Array<Tuple5<T1, T2, T3, T4, T5>>
Pairs the elements of five arrays in an array of Tuple5
.