class Arrays
package thx
Arrays
provides additional extension methods on top of the Array
type.
Note that some of the examples imply using thx.Arrays;
.
Static methods
staticinline after<T> (array:ReadonlyArray<T>, element:T):Array<T>
Finds the first occurrance of element
and returns all the elements after it.
staticall<T> (arr:ReadonlyArray<T>, predicate:T ‑> Bool):Bool
Checks if predicate
returns true for all elements in the array.
staticany<T> (arr:ReadonlyArray<T>, predicate:T ‑> Bool):Bool
Checks if predicate
returns true for at least one element in the array.
staticappend<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.
staticappendIf<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.
staticapplyIndexes<T> (array:ReadonlyArray<T>, indexes:Array<Int>, incrementDuplicates:Bool = false):Array<T>
Arrays.applyIndexes takes an array
and returns a copy of it with its elements rearranged according to indexes
.
If the indexes
array does not contain continuous values, you may want to set incrementDuplicates
to true
.
var result = Arrays.applyIndexes(["B", "C", "A"], [1, 2, 0]); trace(result); // output ["A", "B", "C"]
staticat<T> (arr:ReadonlyArray<T>, indexes:ReadonlyArray<Int>):Array<T>
Creates an array of elements from the specified indexes.
staticatIndex<T> (array:ReadonlyArray<T>, i:Int):Option<T>
Safe indexed access to array elements. Deprecated in favor of getOption
.
staticinline before<T> (array:ReadonlyArray<T>, element:T):Array<T>
Finds the first occurrance of element
and returns all the elements before it.
staticcommonsFromStart<T, PT> (self:ReadonlyArray<T>, other:ReadonlyArray<PT>, ?equality:T ‑> PT ‑> 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.
staticcompare<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
.
staticcontains<T, PT> (array:ReadonlyArray<T>, element:PT, ?eq:T ‑> PT ‑> 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.
staticcontainsAll<T, PT> (array:Array<T>, elements:Iterable<PT>, ?eq:T ‑> PT ‑> 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.
staticcontainsAny<T, PT> (array:ReadonlyArray<T>, elements:Iterable<PT>, ?eq:T ‑> PT ‑> 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.
staticcount<T> (arr:ReadonlyArray<T>):Map<T, Int>
Returns a Map containing the number of occurrances for each value in the array.
staticcreate<T> (length:Int, fillWith:T):Array<T>
Creates a new Array
with length
elements all set to fillWith
.
staticcross<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]]
staticcrossMulti<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]]
staticdistinct<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 ==.
staticdropLeft<T> (a:ReadonlyArray<T>, n:Int):Array<T>
Produces an Array from a[n]
to the last element of a
.
staticdropRight<T> (a:ReadonlyArray<T>, n:Int):Array<T>
Produces an Array from a[0]
to a[a.length-n].
staticdropWhile<T> (a:ReadonlyArray<T>, p:T ‑> Bool):Array<T>
Drops values from Array a
while the predicate returns true.
staticeach<T> (arr:ReadonlyArray<T>, effect:T ‑> Void):Void
Applies a side-effect function to all elements in the array.
staticeachPair<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
.
staticeachi<T> (arr:ReadonlyArray<T>, effect:T ‑> Int ‑> Void):Void
Applies a side-effect function to all elements in the array.
staticequals<T, PT> (a:ReadonlyArray<T>, b:ReadonlyArray<PT>, ?equality:T ‑> PT ‑> Bool):Bool
It compares the lengths and elements of two given arrays and returns true
if all elements match.
An optional equality function can be passed as the last argument. If not provided, strict equality is adopted.
staticextract<T> (a:Array<T>, predicate:T ‑> Bool):T
It finds an element in the array using predicate
and returns it. The element is also
removed from the original array.
If no element satisfies predicate
the array is left unmodified and null
is returned.
staticfilterMap<TIn, TOut> (values:ReadonlyArray<TIn>, f:TIn ‑> Option<TOut>):Array<TOut>
Performs a filter
and map
operation at once. It uses predicate to get either
None
or a transformed value Some
of TOut
.
staticfilterOption<T> (a:ReadonlyArray<Option<T>>):Array<T>
Filters out all None
values from an array and extracts Some(value)
to value
.
staticfind<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.
staticfindIndex<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
.
staticfindLast<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.
staticfindMap<TIn, TOut> (values:ReadonlyArray<TIn>, f:TIn ‑> Option<TOut>):Option<TOut>
Finds the first item in an array where the given function f
returns a Option.Some
value.
If no items map to Some
, None
is returned.
staticfindOption<T> (array:ReadonlyArray<T>, predicate:T ‑> Bool):Option<T>
It returns the first element of the array that matches the predicate function. If none is found it returns null.
staticfindSome<T> (options:ReadonlyArray<Option<T>>):Option<T>
Finds the first item in an Array<Option<T>>
that is Some
, otherwise None
.
staticfindi<T> (array:ReadonlyArray<T>, predicate:T ‑> Int ‑> Bool):Null<T>
Like find
, but each item's index is also passed to the predicate.
staticfindiOption<T> (array:ReadonlyArray<T>, predicate:T ‑> Int ‑> Bool):Option<T>
Like findOption
, but each item's index is also passed to the predicate.
staticinline first<T> (array:ReadonlyArray<T>):Null<T>
It returns the first element of the array or null if the array is empty.
staticinline firstOption<T> (array:ReadonlyArray<T>):Option<T>
It returns an option of the first element or None if the array is empty.
staticinline 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']
staticinline 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]
staticflattenOptions<T> (a:ReadonlyArray<Option<T>>):Option<Array<T>>
Converts an Array<Option<T>>
to Option<Array<T>>
only if all elements in the input
array contain a Some
value. The input and the output array (if any) will have
the same length.
staticinline foldLeft<A, B> (array:ReadonlyArray<A>, init:B, f:B ‑> A ‑> B):B
Alias for reduce that puts the arguments in the proper order.
staticinline foldLeft1<A, B> (array:ReadonlyArray<A>, f:A ‑> A ‑> A):Option<A>
As with foldLeft, but uses first element as Init.
staticfoldLeftEither<A, E, B> (array:ReadonlyArray<A>, init:B, f:B ‑> A ‑> Either<E, B>):Either<E, B>
staticfoldMap<A, B> (array:ReadonlyArray<A>, f:A ‑> B, m:Monoid<B>):B
Fold by mapping the contained values into some monoidal type and reducing with that monoid.
staticfoldS<A> (array:ReadonlyArray<A>, s:Semigroup<A>):Option<A>
Reduce with a semigroup, returning None if the array is empty.
staticinline from<T> (array:ReadonlyArray<T>, element:T):Array<T>
Finds the first occurrance of element
and returns all the elements from that point on.
staticgetOption<T> (array:ReadonlyArray<T>, i:Int):Option<T>
Safe indexed access to array elements.
Null values within array
will also return None
instead of Some(null)
.
staticgroupBy<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
.
staticgroupByAppend<TKey, TValue> (arr:ReadonlyArray<TValue>, resolver:TValue ‑> TKey, map:Map<TKey, Array<TValue>>):Map<TKey, Array<TValue>>
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.
staticgroupByIndex<A, K> (arr:ReadonlyArray<A>, groupKey:Int ‑> K):Map<K, Array<A>>
Group the array by a function of the index.
staticinline hasElements<T> (array:ReadonlyArray<T>):Bool
Returns true
if the array contains at least one element.
staticinline 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
.
staticinline ifEmpty<T> (array:Array<T>, alt:Array<T>):Array<T>
ifEmpty
returns array
if it is neither null
or empty, otherwise it returns alt
staticinline initial<T> (array:ReadonlyArray<T>):Array<T>
Get all the elements from array
except for the last one.
staticintersperse<T> (array:ReadonlyArray<T>, value:T):Array<T>
Creates a new array that alternates the values in array
with value
.
staticinterspersef<T> (array:ReadonlyArray<T>, f:Void ‑> T):Array<T>
Lazy version of intersperse
. It creates a new array that alternates the values in array
with the result of f
.
staticinline isEmpty<T> (array:ReadonlyArray<T>):Bool
It returns true
if the array contains zero elements.
staticinline last<T> (array:ReadonlyArray<T>):Null<T>
It returns the last element of the array or null if the array is empty.
staticinline lastOption<T> (array:ReadonlyArray<T>):Option<T>
It returns an option of the last element, None
if the array is empty.
staticinline map<TIn, TOut> (array:ReadonlyArray<TIn>, callback:TIn ‑> TOut):Array<TOut>
Static wrapper for Array
map
function.
staticmapRight<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.
staticinline 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.
staticmaxBy<A> (arr:ReadonlyArray<A>, ord:Ord<A>):Option<A>
Finds the min element of the array given the specified ordering.
staticminBy<A> (arr:ReadonlyArray<A>, ord:Ord<A>):Option<A>
Finds the min element of the array given the specified ordering.
staticorder<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.
staticpad<T> (arr:ReadonlyArray<T>, len:Int, ?def:T):Array<T>
Pads out to len with optional default def
, ignores if len is less than Array length.
staticpartition<T> (arr:ReadonlyArray<T>, f:T ‑> Bool):Tuple2<Array<T>, Array<T>>
Produces a Tuple2
containing two Array
, the left being elements where f(e) == true
, the rest in the right.
staticpartitionWhile<T> (arr:ReadonlyArray<T>, f:T ‑> Bool):Tuple2<Array<T>, Array<T>>
staticpull<T, PT> (array:Array<T>, toRemove:ReadonlyArray<PT>, ?equality:T ‑> PT ‑> Bool):Void
Pulls from array
all occurrences of all the elements in toRemove
. Optionally takes
an equality
function.
staticpushIf<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.
staticrank<T> (array:ReadonlyArray<T>, compare:T ‑> T ‑> Int, incrementDuplicates:Bool = true):Array<Int>
Given an array of values, it returns an array of indexes permutated applying the function compare
.
By default rank
will return continuous values. If you know that your set does not contain duplicates you might want to turn off that feature by setting incrementDuplicates
to false
.
var arr = ["C","A","B"];
var indexes = Arrays.rank(arr, Strings.compare);
trace(indexes); // output [2,0,1]
staticreduce<A, B> (array:ReadonlyArray<A>, f:B ‑> A ‑> B, initial:B):B
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.
staticinline reduceRight<A, B> (array:ReadonlyArray<A>, f:B ‑> A ‑> B, initial:B):B
Same as Arrays.reduce
but starting from the last element and traversing to the first
staticreducei<A, B> (array:ReadonlyArray<A>, f:B ‑> A ‑> Int ‑> B, initial:B):B
It is the same as reduce
but with the extra integer index
parameter.
staticremoveAll<T, PT> (array:Array<T>, element:PT, ?equality:T ‑> PT ‑> Bool):Void
Remove every occurrance of element
from array
. If equality
is not specified, strict equality
will be adopted.
staticresize<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.
staticresized<T> (array:Array<T>, length:Int, fill:T):Array<T>
Copies and 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 creates and returns a copy of the passed array.
staticinline rest<T> (array:ReadonlyArray<T>):Array<T>
Returns all but the first element of the array
staticinline reversed<T> (array:ReadonlyArray<T>):Array<T>
Creates a copy of the array with its elements in reverse order.
staticrotate<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]]
.
staticinline sample<T> (array:ReadonlyArray<T>, n:Int):Array<T>
Returns n
elements at random from the array. Elements will not be repeated.
staticinline sampleOne<T> (array:ReadonlyArray<T>):Null<T>
Returns one element at random from the array or null if the array is empty.
staticshuffle<T> (a:ReadonlyArray<T>):Array<T>
It returns a copy of the array with its elements randomly changed in position.
staticsplit<T> (array:ReadonlyArray<T>, parts:Int):Array<Array<T>>
Splits an array into a specified number of parts
.
staticsplitBy<T> (array:ReadonlyArray<T>, len:Int):Array<Array<T>>
Splits an array into smaller arrays at most of length equal to len
.
staticsplitByPad<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.
staticinline tail<T> (array:ReadonlyArray<T>):Array<T>
It returns the elements of the array after the first.
staticinline take<T> (arr:ReadonlyArray<T>, n:Int):Array<T>
Returns the first n
elements from the array.
staticinline takeLast<T> (arr:ReadonlyArray<T>, n:Int):Array<T>
Returns the last n
elements from the array.
statictoMap<K, V> (arr:ReadonlyArray<Tuple<K, V>>, keyOrder:Ord<K>):VNel<K, Map<K, V>>
Convert an array of tuples to a map. If there are collisions between keys, return an error.
statictraverseEither<E, T, U> (arr:ReadonlyArray<T>, f:T ‑> Either<E, U>):Either<E, Array<U>>
Traverse the array with a function that may return values wrapped in Either. If any result is in Left, the first such value is returned; if all results are in Right, then the array of those results is returned in Right.
If you want to instead collect errors rather than fail on the first error, see traverseValidation.
statictraverseOption<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.
statictraverseValidation<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.
statictraverseValidationIndexed<E, T, U> (arr:ReadonlyArray<T>, f:T ‑> Int ‑> 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.
staticunzip<T1, T2> (array:ReadonlyArray<Tuple2<T1, T2>>):Tuple2<Array<T1>, Array<T2>>
Unzip an array of Tuple2
staticunzip3<T1, T2, T3> (array:ReadonlyArray<Tuple3<T1, T2, T3>>):Tuple3<Array<T1>, Array<T2>, Array<T3>>
Unzip an array of Tuple3
staticunzip4<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
staticunzip5<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
staticinline with<T> (arr:ReadonlyArray<T>, el:T):ReadonlyArray<T>
Returns a copy of the array with the new element added to the end.
staticwithInsert<T> (arr:ReadonlyArray<T>, el:T, pos:Int):ReadonlyArray<T>
Returns a copy of the array with the new element inserted at position pos
.
staticinline withPrepend<T> (arr:ReadonlyArray<T>, el:T):ReadonlyArray<T>
Returns a copy of the array with the new element added to the beginning.
staticwithSlice<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.
staticzip<T1, T2> (array1:ReadonlyArray<T1>, array2:ReadonlyArray<T2>):Array<Tuple2<T1, T2>>
Pairs the elements of two arrays in an array of Tuple2
.
staticzip2Ap<A, B, C> (f:A ‑> B ‑> C, ax:ReadonlyArray<A>, bx:ReadonlyArray<B>):Array<C>
Zip two arrays by applying the provided function to the aligned members.
staticzip3<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
.
staticzip3Ap<A, B, C, D> (f:A ‑> B ‑> C ‑> D, ax:ReadonlyArray<A>, bx:ReadonlyArray<B>, cx:ReadonlyArray<C>):Array<D>
Zip three arrays by applying the provided function to the aligned members.
staticzip4<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
.
staticzip4Ap<A, B, C, D, E> (f:A ‑> B ‑> C ‑> D ‑> E, ax:ReadonlyArray<A>, bx:ReadonlyArray<B>, cx:ReadonlyArray<C>, dx:ReadonlyArray<D>):Array<E>
Zip four arrays by applying the provided function to the aligned members.
staticzip5<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
.
staticzip5Ap<A, B, C, D, E, F> (f:A ‑> B ‑> C ‑> D ‑> E ‑> F, ax:ReadonlyArray<A>, bx:ReadonlyArray<B>, cx:ReadonlyArray<C>, dx:ReadonlyArray<D>, ex:ReadonlyArray<E>):Array<F>
Zip five arrays by applying the provided function to the aligned members.
staticzipAp<A, B> (ax:ReadonlyArray<A>, fx:ReadonlyArray<A ‑> B>):Array<B>
The 'zip' applicative functor operation.