Array class

From Catglobe Wiki
Revision as of 11:14, 19 October 2018 by Administrator (talk | contribs)
Jump to: navigation, search

Result:

Array



The array object

Constructors

  • () - Creates an empty array
  • (array toCopy "Array to copy") - Creates a copy of another array
  • (int count "Number of items to put in the array") - Creates an array with a predefined size, where all elements are Empty

Methods

  • Empty Add(object element "element to add") - Add a new element to the Array
  • Empty AddRange(array source "Array to get elements from.") - Add all elements of another array to this array.
  • bool Any(Function predicate "A function that takes 1 parameter of the types in the array, and return true/false") - Return true if the array contains the element using the given function.
  • bool Contains(object element "Element to check for") - Return true if the array contains the element using the normal equal operator.
  • array Except(array elements "Elements to remove") - Return all elements that does not exist in the other collection.
  • object First(Function selector "A function to test each element for a condition.") - Returns the first element in a sequence that satisfies a specified condition.
  • object FirstOrDefault(Function selector "A function to test each element for a condition.") - Returns the first element in a sequence that satisfies a specified condition or empty if not found.
  • int Frequency(number number "The number to search for") - Counts the number of times a given Number object exists in the Array. Can only use if all the elements are of type Number
  • object this[] { get; }(int index "Index") - Get element at index
  • bool IsCharacterArray() - check if array is an array of characters
  • bool IsNumericArray() - check if array is an array of integer numbers
  • bool IsStringArray() - check if array is an array of string
  • array OrderBy(Function comparer "Function that compares two objects of the same type. Must return a signed integer that indicates the relative values of first param A and second param B. Value Less than 0 : A is less than B.Value 0 : A equals B.Value Greater than 0 : A is greater than B.It can also be a function that takes 1 parameter and returns a string or number.") - Sorts the elements of a sequence in ascending order by using a specified comparer.
  • Empty Randomize() - Randomize the order of the elements in the current array.
  • Empty RemoveItemAt(int index "The index to remove the object from") - Remove an element from the Array
  • array Reverse() - Returns an array with all the elements in the opposite order.
  • array Select(Function selector "A transform function to apply to each element.") - Projects each element of a sequence into a new form.
  • array SelectMany(Function selector "A transform function to get each sub array.") - Projects each array element of a sequence into a new form.
  • Empty this[] { set; }(int index "Index", object value "Value to set") - Set element value at index
  • array Skip(int n "How many elements to skip") - Get all but the n first elements.
  • array Take(int n "How many elements to take") - Get the n first elements.
  • Dictionary ToDictionary(Function keySelector "A transform function to get the key of each element.") - Return a dictionary with the elements of the array.
  • Dictionary ToDictionary(Function keySelector "A transform function to get the key of each element.", Function valueSelector "A transform function to get the value of each element.") - Return a dictionary with the elements of the array.
  • string ToString() - The string representation of the object.
  • array Where(Function predicate "A function that takes 1 parameter of the types in the array and return true/false") - Filters a sequence of values based on a predicate.

Properties

  • number Average { get; } - Average of the objects in the Array object. Can only use if all the elements are of type Number
  • int Count { get; } - Number of elements in Array
  • number Max { get; } - Largest of all the objects in the Array object. Can only use if all the elements are of type Number
  • number Min { get; } - Smallest of all the objects in the Array object. Can only use if all the elements are of type Number
  • string ObjectTypeName { get; } - The name of the type of object.
  • number Sum { get; } - Sum of all the objects in the Array object. Can only use if all the elements are of type Number
  • TypeInformation TypeInformation { get; } - Get information about this class.


Examples

array x = new Array();
print(x); // {}


array x = new Array(10);
print(x); // {Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty,Empty}


array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { a;})
// .Select(function (string a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'String'
// .Select(function (array a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'Array'
// .Select(function (bool a) { a;}) -> will not work: Cannot assign a value of type 'Number' to a variable of type 'Boolean'
// .Select(function () { a;}) -> will not work: Function called with too many parameters @
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;});
print(y);


array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { 88888;})
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;});
print(y);


array x = {10,20,30,40,50,60,70,80,90,100};
array y = x
.Select(function (number a) { a;})
.Where(function (number a) {number i = getRandomNumber(0,10)*10; i > 40;})
.OrderBy(function(number a, number b) {if (a>b) 1; else if (a<b) -1; else 0;});
// .OrderBy(function(number a, number b){if (a>b) -1; else if (a<b) 1; else 0;});
print(y);


array x = {{1,2,3},{4,false,6,7,8},{10,"ab",{"aaa",11,true},13,14},{"aa","bb",17},{},{true,88}};
array y = x
.Select(function (array a) {; a;})
.Where(function (array a) {arrayCount(a) > 3;});
print(y); // {{4,False,6,7,8},{10,ab,{aaa,11,True},13,14}}


array y = {20,30,40,50,70,80,90,100};
print(y.FirstOrDefault(function (number a) { true; })); // 20
print(y.FirstOrDefault(function (number a) { a > 70; })); // 80
print(y.First(function(number a) { true; })); // 20
print(y.First(function(number a) { a > 40; })); // 50


array a = {1,2,3,4,5,6,7,8};
a.Randomize();
print(a); // {1,5,2,8,7,6,4,3}


//Example for using a.Except
array a = {1,2,3,4,5,6,7,8,9};
array b = a.Except({1,3,5,7,9});
print(a);//{1,2,3,4,5,6,7,8,9}
print(b);//{2,4,6,8}
//Example for using .Any and .Reverse()
   object e;
   array lastPersonsUsed = empty;
   try { 
     UserPreference pref = new UserPreference("Task_Add", 1);
     lastPersonsUsed = Catglobe.Json.Parse(pref.Value);
   } catch (e) {  }
   if (lastPersonsUsed == empty)
     lastPersonsUsed = {};
   lastPersonsUsed = lastPersonsUsed.Reverse();
   lastPersonsUsed.Add({123, "Username"});
   array fixedLastPersonsUsed;

   lastPersonsUsed = lastPersonsUsed.Reverse();
   number cur = getCurrentUserUniqueId();
   for (i for 0; lastPersonsUsed.Count) {
      number userResourceId = lastPersonsUsed[i][0];
      if (cur != userResourceId && userResourceId != 0 && !fixedLastPersonsUsed.Any(function(array a){return userResourceId == a[0];}))
         fixedLastPersonsUsed.Add(lastPersonsUsed[i]);
   }
   new UserPreference("Task_Add", 1, Catglobe.Json.Encode(fixedLastPersonsUsed.Take(5)));