Difference between revisions of "Array class"

From Catglobe Wiki
Jump to: navigation, search
Line 1: Line 1:
 
{{CGscriptClass_Template
 
{{CGscriptClass_Template
|Name=<nowiki>Array</nowiki>
+
|Name=Array
|Description=<nowiki>The array object</nowiki>
+
|Description=The array object
 
|Constructors=
 
|Constructors=
{{CGscriptConstructors_Template|Description=<nowiki>Creates an empty array</nowiki>}}
+
{{CGscriptConstructors_Template|Description=Creates an empty array}}
 
{{CGscriptConstructors_Template|Parameters=
 
{{CGscriptConstructors_Template|Parameters=
{{CGscriptParameters_Template|Type=array|Name=<nowiki>toCopy</nowiki>|Description=<nowiki>Array to copy</nowiki>}}
+
{{CGscriptParameters_Template|Type=array|Name=toCopy|Description=Array to copy}}
|Description=<nowiki>Creates a copy of another array</nowiki>}}
+
|Description=Creates a copy of another array}}
 
{{CGscriptConstructors_Template|Parameters=
 
{{CGscriptConstructors_Template|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>count</nowiki>|Description=<nowiki>Number of items to put in the array</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=count|Description=Number of items to put in the array}}
|Description=<nowiki>Creates an array with a predefined size, where all elements are Empty</nowiki>}}
+
|Description=Creates an array with a predefined size, where all elements are Empty}}
 
|Methods=
 
|Methods=
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Add</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=Empty|Name=Add|Parameters=
{{CGscriptParameters_Template|Type=AnyType|Name=<nowiki>element</nowiki>|Description=<nowiki>element to add</nowiki>}}
+
{{CGscriptParameters_Template|Type=AnyType|Name=element|Description=element to add}}
|Description=<nowiki>Add a new element to the Array</nowiki>}}
+
|Description=Add a new element to the Array}}
{{CGscriptMethods_Template|ReturnType=AnyType|Name=<nowiki>First</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=AnyType|Name=First|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A function to test each element for a condition.</nowiki>}}
+
{{CGscriptParameters_Template|Type=Function|Name=selector|Description=A function to test each element for a condition.}}
|Description=<nowiki>Returns the first element in a sequence that satisfies a specified condition.</nowiki>}}
+
|Description=Returns the first element in a sequence that satisfies a specified condition.}}
{{CGscriptMethods_Template|ReturnType=AnyType|Name=<nowiki>FirstOrDefault</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=AnyType|Name=FirstOrDefault|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A function to test each element for a condition.</nowiki>}}
+
{{CGscriptParameters_Template|Type=Function|Name=selector|Description=A function to test each element for a condition.}}
|Description=<nowiki>Returns the first element in a sequence that satisfies a specified condition or empty if not found.</nowiki>}}
+
|Description=Returns the first element in a sequence that satisfies a specified condition or empty if not found.}}
{{CGscriptMethods_Template|ReturnType=number|Name=<nowiki>Frequency</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=number|Name=Frequency|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>number</nowiki>|Description=<nowiki>The number to search for</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=number|Description=The number to search for}}
|Description=<nowiki>Counts the number of times a given Number object exists in the Array. Can only use if all the elements are of type Number</nowiki>}}
+
|Description=Counts the number of times a given Number object exists in the Array. Can only use if all the elements are of type Number}}
{{CGscriptMethods_Template|ReturnType=AnyType|Name=<nowiki>this[] { get; }</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=AnyType|Name=this[] { get; }|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>index</nowiki>|Description=<nowiki>Index</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=index|Description=Index}}
|Description=<nowiki>Get element at index</nowiki>}}
+
|Description=Get element at index}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsCharacterArray</nowiki>|Description=<nowiki>check if array is an array of characters</nowiki>}}
+
{{CGscriptMethods_Template|ReturnType=bool|Name=IsCharacterArray|Description=check if array is an array of characters}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsNumericArray</nowiki>|Description=<nowiki>check if array is an array of numbers</nowiki>}}
+
{{CGscriptMethods_Template|ReturnType=bool|Name=IsNumericArray|Description=check if array is an array of numbers}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsNumericArray</nowiki>|Description=<nowiki>check if array is an array of integer numbers</nowiki>}}
+
{{CGscriptMethods_Template|ReturnType=bool|Name=IsNumericArray|Description=check if array is an array of integer numbers}}
{{CGscriptMethods_Template|ReturnType=bool|Name=<nowiki>IsStringArray</nowiki>|Description=<nowiki>check if array is an array of string</nowiki>}}
+
{{CGscriptMethods_Template|ReturnType=bool|Name=IsStringArray|Description=check if array is an array of string}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>OrderBy</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=array|Name=OrderBy|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>comparer</nowiki>|Description=<nowiki>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.</nowiki>}}
+
{{CGscriptParameters_Template|Type=Function|Name=comparer|Description=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.}}
|Description=<nowiki>Sorts the elements of a sequence in ascending order by using a specified comparer.</nowiki>}}
+
|Description=Sorts the elements of a sequence in ascending order by using a specified comparer.}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>RemoveItemAt</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=Empty|Name=RemoveItemAt|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>index</nowiki>|Description=<nowiki>The index to remove the object from</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=index|Description=The index to remove the object from}}
|Description=<nowiki>Remove an element from the Array</nowiki>}}
+
|Description=Remove an element from the Array}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Select</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=array|Name=Select|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>selector</nowiki>|Description=<nowiki>A transform function to apply to each element.</nowiki>}}
+
{{CGscriptParameters_Template|Type=Function|Name=selector|Description=A transform function to apply to each element.}}
|Description=<nowiki>Projects each element of a sequence into a new form.</nowiki>}}
+
|Description=Projects each element of a sequence into a new form.}}
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>this[] { set; }</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=Empty|Name=this[] { set; }|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>index</nowiki>|Description=<nowiki>Index</nowiki>|Comma=,}}{{CGscriptParameters_Template|Type=AnyType|Name=<nowiki>value</nowiki>|Description=<nowiki>Value to set</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=index|Description=Index|Comma=,}}{{CGscriptParameters_Template|Type=AnyType|Name=value|Description=Value to set}}
|Description=<nowiki>Set element value at index</nowiki>}}
+
|Description=Set element value at index}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Skip</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=array|Name=Skip|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>n</nowiki>|Description=<nowiki>How many elements to skip</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=n|Description=How many elements to skip}}
|Description=<nowiki>Get all but the n first elements.</nowiki>}}
+
|Description=Get all but the n first elements.}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Take</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=array|Name=Take|Parameters=
{{CGscriptParameters_Template|Type=number|Name=<nowiki>n</nowiki>|Description=<nowiki>How many elements to take</nowiki>}}
+
{{CGscriptParameters_Template|Type=number|Name=n|Description=How many elements to take}}
|Description=<nowiki>Get the n first elements.</nowiki>}}
+
|Description=Get the n first elements.}}
{{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>ToString</nowiki>|Description=<nowiki>The string representation of the object.</nowiki>}}
+
{{CGscriptMethods_Template|ReturnType=string|Name=ToString|Description=The string representation of the object.}}
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>Where</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=array|Name=Where|Parameters=
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 1 parameter of the types in the array and return true/false</nowiki>}}
+
{{CGscriptParameters_Template|Type=Function|Name=predicate|Description=A function that takes 1 parameter of the types in the array and return true/false}}
|Description=<nowiki>Filters a sequence of values based on a predicate.</nowiki>}}
+
|Description=Filters a sequence of values based on a predicate.}}
 +
{{CGscriptParameters_Template|Type=array|Name=Randomize|Description=Random shuffling of an array}}
 +
 
 
|Properties=
 
|Properties=
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Average</nowiki>|HasGetter=1|Description=<nowiki>Average of the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=number|Name=Average|HasGetter=1|Description=Average of the objects in the Array object. Can only use if all the elements are of type Number}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Count</nowiki>|HasGetter=1|Description=<nowiki>Number of elements in Array</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=number|Name=Count|HasGetter=1|Description=Number of elements in Array}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Max</nowiki>|HasGetter=1|Description=<nowiki>Largest of all the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=number|Name=Max|HasGetter=1|Description=Largest of all the objects in the Array object. Can only use if all the elements are of type Number}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Min</nowiki>|HasGetter=1|Description=<nowiki>Smallest of all the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=number|Name=Min|HasGetter=1|Description=Smallest of all the objects in the Array object. Can only use if all the elements are of type Number}}
{{CGscriptProperties_Template|ReturnType=string|Name=<nowiki>ObjectTypeName</nowiki>|HasGetter=1|Description=<nowiki>The name of the type of object.</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=string|Name=ObjectTypeName|HasGetter=1|Description=The name of the type of object.}}
{{CGscriptProperties_Template|ReturnType=number|Name=<nowiki>Sum</nowiki>|HasGetter=1|Description=<nowiki>Sum of all the objects in the Array object. Can only use if all the elements are of type Number</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=number|Name=Sum|HasGetter=1|Description=Sum of all the objects in the Array object. Can only use if all the elements are of type Number}}
{{CGscriptProperties_Template|ReturnType=TypeInformation|Name=<nowiki>TypeInformation</nowiki>|HasGetter=1|Description=<nowiki>Get information about this class.</nowiki>}}
+
{{CGscriptProperties_Template|ReturnType=TypeInformation|Name=TypeInformation|HasGetter=1|Description=Get information about this class.}}
 
}}
 
}}
  
=== <span style="color:#DF8621;">'''Examples'''</span> ===
+
=== <span style="color:#DF8621">'''Examples'''</span> ===
  
 
<source lang="javascript">array x = new Array();
 
<source lang="javascript">array x = new Array();
Line 93: Line 95:
 
print(y.First(function(number a) { true; })); // 20
 
print(y.First(function(number a) { true; })); // 20
 
print(y.First(function(number a) { a > 40; })); // 50</source>
 
print(y.First(function(number a) { a > 40; })); // 50</source>
 +
 +
<br/><source lang="javascript">array a = {1,2,3,4,5,6,7,8};
 +
a.Randomize();
 +
print(a); // {1,5,2,8,7,6,4,3}</source>

Revision as of 09:56, 13 November 2015

Array



The array object

Constructors

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

Methods

  • Empty Add(AnyType element "element to add") - Add a new element to the Array
  • AnyType First(Function selector "A function to test each element for a condition.") - Returns the first element in a sequence that satisfies a specified condition.
  • AnyType 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.
  • number 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
  • AnyType this[] { get; }(number 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 numbers
  • 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.") - Sorts the elements of a sequence in ascending order by using a specified comparer.
  • Empty RemoveItemAt(number index "The index to remove the object from") - Remove an element from the Array
  • array Select(Function selector "A transform function to apply to each element.") - Projects each element of a sequence into a new form.
  • Empty this[] { set; }(number index "Index", AnyType value "Value to set") - Set element value at index
  • array Skip(number n "How many elements to skip") - Get all but the n first elements.
  • array Take(number n "How many elements to take") - Get the n first elements.
  • 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.

array Randomize "Random shuffling of an array"

Properties

  • number Average { get; } - Average of the objects in the Array object. Can only use if all the elements are of type Number
  • number 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}