UNPKG

qcobjects-docs

Version:

The official app and website for documentation of QCObjects

263 lines (204 loc) 4.93 kB
## List and Math Functions ### ArrayList A Class definition used to manage lists ```javascript let myvar = New(ArrayList,[1,2,3]); ``` ### ArrayCollection An extended definition for advanced handling of collections ```javascript let collection = New(ArrayCollection, {source:[0,1,2]}); ``` ### [ArrayList or Array].unique Filters an Array or an ArrayList object to obtain only unique elements. NOTE: It only filters a single value sequence. ```javascript let my_unique_list = [0,1,2,1,2].unique() // will result in: my_unique_list = [ 0, 1, 2 ] ``` ### [ArrayList or Array].table This is intended for shell scripts use only. It shows a table of the values in the list. ```javsascript ["a","b","c","d"].table() // it will show ┌─────────┬────────┐ │ (index) │ Values │ ├─────────┼────────┤ │ 0'a' │ │ 1'b' │ │ 2'c' │ │ 3'd' │ └─────────┴────────┘ ``` ### [ArrayList or Array].sort Sorts the elements of the array or list. ```javascript let my_sorted_array = [3,3,4,0,2,1].sort() // my_sorted_array = [ 0, 1, 2, 3, 3, 4 ] ``` ```javascript let my_sorted_list = New(ArrayList,{source:[3,3,4,0,2,1]}).source.sort() // my_sorted_list = [ 0, 1, 2, 3, 3, 4 ] ``` ### [ArrayList or Array].sortBy Sorts a list of objects by a property value. ```javascript let my_ordered_list = [ {"b":1,"a":2}, {"b":2,"a":1}, {"b":3,"a":3}, ].sortBy("a") // it will result in [ { b: 2, a: 1 }, { b: 1, a: 2 }, { b: 3, a: 3 } ] ``` ### [ArrayList or Array].matrix Generates a matrix in one dimension. #### Usage **[].matrix (length, [value])** Where **length** is the number of elements and the optional **value** is the value of each element, it can be any value of any type. ```javascript let matrix = Array.matrix(10); // matrix = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ``` ```javascript let matrix = ArrayList.matrix(10,1); // matrix = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] ``` ```javascript let a = 1, b = 2; let c = ArrayList.matrix(10,{a,b}) // c = [ { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 }, { a: 1, b: 2 } ] ``` ### [ArrayList or Array].matrix2d Creates a 2D matrix. ```javsascript let matrix2d = ArrayList.matrix2d(2,1); // [ [ 1, 1 ], [ 1, 1 ] ] ``` ### [ArrayList or Array].matrix3d Creates a 3D matrix. ```javascript let matrix3d = ArrayList.matrix3d(3,"a"); // it will result in a 3x3 matrix with the value "a" on every element [ [ [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ] ], [ [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ] ], [ [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ] ] ] ``` ### range Python like function to create a range list. You can use it in conjunction with ArrayList.matrix, ArrayList.matrix2d and ArrayList.matrix3d to generate complex matrix ranges. #### Usage range(length) or range(initialIndex, finalIndex) range() without any params returns an empty list range(0) returns a list with one element with value 0 ```javascript logger.debugEnabled=true; for (var i in range(10)){ (!isNaN(i) && logger.debug(i)) } // the above code will show [DEBUG] 0 [DEBUG] 1 [DEBUG] 2 [DEBUG] 3 [DEBUG] 4 [DEBUG] 5 [DEBUG] 6 [DEBUG] 7 [DEBUG] 8 [DEBUG] 9 [DEBUG] 10 ``` ```javascript logger.debugEnabled=true; // same result will be obtained iterating the range first for (var i in {...range(10)}){ logger.debug(i) } // the above code will show [DEBUG] 0 [DEBUG] 1 [DEBUG] 2 [DEBUG] 3 [DEBUG] 4 [DEBUG] 5 [DEBUG] 6 [DEBUG] 7 [DEBUG] 8 [DEBUG] 9 [DEBUG] 10 ``` ```javascript // a bit shorter syntax for the same result range(10).map(n=>logger.debug(n)) ``` ```javascript let normalizedMatrix = ArrayList.matrix(3,range(2)); // normalizedMatrix = [ [ 0, 1, 2 ], [ 0, 1, 2 ], [ 0, 1, 2 ] ] ``` ```javascript let my3dmatrix = ArrayList.matrix3d(3,range(0,1)); // my3dmatrix will be [ [ [ [0, 1], [0, 1], [0, 1] ], [ [0, 1], [0, 1], [0, 1] ], [ [0, 1], [0, 1], [0, 1] ] ], [ [ [0, 1], [0, 1], [0, 1] ], [ [0, 1], [0, 1], [0, 1] ], [ [0, 1], [0, 1], [0, 1] ] ], [ [ [0, 1], [0, 1], [0, 1] ], [ [0, 1], [0, 1], [0, 1] ], [ [0, 1], [0, 1], [0, 1] ] ] ] ``` ### Array.sum Sums the elements of an array. ```javascript let s = [1,2,3].sum() // s = 6 ``` ### Array.avg Computes the average value of the elements in the Array ```javascript let average = [10,5].avg() // average = 7.5 ``` ### Array.min Returns the min value from the elements of an Array. ```javascript let minValue = [1,2,3].min() // minValue = 1 ``` ### Array.max Returns the max value from the elements of an Array ```javascript let maxValue = [1,2,3].max() // maxValue = 3 ```