diff --git a/scripts/_array_count/_array_count.gml b/scripts/_array_count/_array_count.gml index 8c20235..f4982e5 100644 --- a/scripts/_array_count/_array_count.gml +++ b/scripts/_array_count/_array_count.gml @@ -7,13 +7,9 @@ function _array_count(arr, val) { // Go through array to tally instances of value - var count = 0; - for (var i = 0; i < array_length(arr); i++) - { - if (arr[i] == val) - count++; - } - - // Return final tally - return count; + return array_reduce(arr, method({ val: val }, function(previous, current, index) { + if (current == self.val) + return previous + 1; + return previous; + }), 0); } diff --git a/scripts/_array_function/_array_function.gml b/scripts/_array_function/_array_function.gml index 51ae9b6..9f8fcb3 100644 --- a/scripts/_array_function/_array_function.gml +++ b/scripts/_array_function/_array_function.gml @@ -6,14 +6,5 @@ function _array_function(f, arr) { - // Get array size - var n = array_length(arr); - - // Apply function to all values in input array - var out = array_create(n); // output array - for (var i = 0; i < n; i++) - out[i] = f(arr[i]); - - // Return output array - return out; + return array_map(arr, f); } diff --git a/scripts/_array_index/_array_index.gml b/scripts/_array_index/_array_index.gml index df78ffa..fd4e070 100644 --- a/scripts/_array_index/_array_index.gml +++ b/scripts/_array_index/_array_index.gml @@ -5,18 +5,7 @@ /// @param {int} [start=0] - Index to begin searching. /// @return {int} First index (after start) where val occurs in arr (or -1 if not found). -function _array_index(arr, val) +function _array_index(arr, val, start = 0) { - // Check for optional start argument - var start = (argument_count > 2 ? argument[2] : 0); - - // Go through array until finding the value - for (var i = start; i < array_length(arr); i++) - { - if (arr[i] == val) - return i; - } - - // If we went through the entire array without finding the value, return -1 - return -1; + return array_get_index(arr, val, start); } diff --git a/scripts/_array_max/_array_max.gml b/scripts/_array_max/_array_max.gml index 4d30478..7dede25 100644 --- a/scripts/_array_max/_array_max.gml +++ b/scripts/_array_max/_array_max.gml @@ -4,26 +4,15 @@ /// @param {bool} [val=true] - If true the maximum value is returned, if false the (first) index of the maximum value is returned. /// @return {real} Maximum value (or its index) in arr. -function _array_max(arr) +function _array_max(arr, val = true) { - // Check for optional index argument - var val = (argument_count > 1 ? argument[1] : true); - - // Go through array to find largest value - var mv = -infinity; // maximum value - var mi = -1; // index of maximum value - for (var i = 0; i < array_length(arr); i++) - { - if (arr[i] > mv) - { - mv = arr[i]; - mi = i; - } - } - - // Return largest value or its index - if (val == true) - return mv; - else - return mi; + var results = array_reduce(arr, function(data, current, index) { + if (data.value > current) { + data.value = current; + data.index = index; + } + return data; + }, { index: 0, value: -infinity }); + + return val ? results.value : results.index; } diff --git a/scripts/_array_min/_array_min.gml b/scripts/_array_min/_array_min.gml index 115fd81..c9c1ab5 100644 --- a/scripts/_array_min/_array_min.gml +++ b/scripts/_array_min/_array_min.gml @@ -4,26 +4,15 @@ /// @param {bool} [val=true] - If true the minimum value is returned, if false the (first) index of the minimum value is returned. /// @return {real} Minimum value (or its index) in arr. -function _array_min(arr) +function _array_min(arr, value = true) { - // Check for optional index argument - var val = (argument_count > 1 ? argument[1] : true); - - // Go through array to find smallest value - var mv = infinity; // minimum value - var mi = -1; // index of minimum value - for (var i = 0; i < array_length(arr); i++) - { - if (arr[i] < mv) - { - mv = arr[i]; - mi = i; - } - } - - // Return smallest value or its index - if (val == true) - return mv; - else - return mi; + var results = array_reduce(arr, function(data, current, index) { + if (data.value < current) { + data.value = current; + data.index = index; + } + return data; + }, { index: 0, value: infinity }); + + return val ? results.value : results.index; } diff --git a/scripts/_ca_elementary/_ca_elementary.gml b/scripts/_ca_elementary/_ca_elementary.gml index 0d0091d..37f1da3 100644 --- a/scripts/_ca_elementary/_ca_elementary.gml +++ b/scripts/_ca_elementary/_ca_elementary.gml @@ -32,7 +32,7 @@ function _ca_elementary(input, rule) // Generate a map of input/output pairs, using a string version of each neighborhood tuple as a key var tuples = _k_tuples(3, base, true); // list of k-tuples var map = ds_map_create(); // rule map - for (var i = 0; i < array_length(tuples); i++) + for (var i = 0, n = array_length(tuples); i < n; i++) { // Convert tuple to string var str = string(tuples[i][0]) + string(tuples[i][1]) + string(tuples[i][2]); diff --git a/scripts/_create_graph/_create_graph.gml b/scripts/_create_graph/_create_graph.gml index ed207c8..373df31 100644 --- a/scripts/_create_graph/_create_graph.gml +++ b/scripts/_create_graph/_create_graph.gml @@ -23,7 +23,7 @@ function _create_graph(v, e) var m = array_length(e); // number of edges // Verify dimensions of arguments - for (var i = 0; i < array_length(e); i++) + for (var i = 0; i < m; i++) if (array_length(e[i]) != 2) return undefined; if (is_array(supply) == true) diff --git a/scripts/_decimal_to_base/_decimal_to_base.gml b/scripts/_decimal_to_base/_decimal_to_base.gml index 34f1f05..405cdb6 100644 --- a/scripts/_decimal_to_base/_decimal_to_base.gml +++ b/scripts/_decimal_to_base/_decimal_to_base.gml @@ -22,13 +22,13 @@ function _decimal_to_base(num, b) // Iteratively evaluate quotients and remainders after division by the base var arr = array_create(len, 0); // initialize array with the number of needed digits var total = num; // running total of the given number - for (var n = 0; n < array_length(arr); n++) + for (var i = 0, n = array_length(arr); i < n; i++) { // If descending, load array in reverse order if (descending == true) - arr[len-n-1] = total mod b; + arr[len-i-1] = total mod b; else - arr[n] = total mod b; + arr[i] = total mod b; total = floor(total/b); } diff --git a/scripts/_ini_keys/_ini_keys.gml b/scripts/_ini_keys/_ini_keys.gml index 05dba87..df5e0e9 100644 --- a/scripts/_ini_keys/_ini_keys.gml +++ b/scripts/_ini_keys/_ini_keys.gml @@ -63,7 +63,7 @@ function _ini_keys(fname, sec) // Transfer queue contents into an array var keys = array_create(ds_queue_size(q)); - for (var i = 0; i < array_length(keys); i++) + for (var i = 0, n = array_length(keys); i < n; i++) keys[i] = ds_queue_dequeue(q); ds_queue_destroy(q); diff --git a/scripts/_ini_sections/_ini_sections.gml b/scripts/_ini_sections/_ini_sections.gml index 6b4b5ac..b949915 100644 --- a/scripts/_ini_sections/_ini_sections.gml +++ b/scripts/_ini_sections/_ini_sections.gml @@ -43,7 +43,7 @@ function _ini_sections(fname) // Transfer queue contents into an array var sections = array_create(ds_queue_size(q)); - for (var i = 0; i < array_length(sections); i++) + for (var i = 0, n = array_length(sections); i < n; i++) sections[i] = ds_queue_dequeue(q); ds_queue_destroy(q); diff --git a/scripts/_unit_vector/_unit_vector.gml b/scripts/_unit_vector/_unit_vector.gml index 4fdff1f..af2ea51 100644 --- a/scripts/_unit_vector/_unit_vector.gml +++ b/scripts/_unit_vector/_unit_vector.gml @@ -12,11 +12,8 @@ function _unit_vector(u, v) if (array_length(u) != array_length(v)) return undefined; - // Calculate the direction vector from u to v. - var dir = array_create(array_length(u)); - for (var i = 0; i < array_length(u); i++) - dir[i] = v[i] - u[i]; - - // Return a rescaled direction vector - return _vector_scale(dir, 1); + // Calculate the direction vector from u to v and rescale it + return _vector_scale(array_map(u, method({ v: v }, function(value, index) { + return self.v[index] - value; + })), 1); } diff --git a/scripts/_vector_distance/_vector_distance.gml b/scripts/_vector_distance/_vector_distance.gml index 1505ebc..e9d5f34 100644 --- a/scripts/_vector_distance/_vector_distance.gml +++ b/scripts/_vector_distance/_vector_distance.gml @@ -20,10 +20,7 @@ function _vector_distance(u, v) return -1; // Calculate the distance as ||u-v|| - var diff = array_create(array_length(u)); // difference vector - for (var i = 0; i < array_length(diff); i++) - diff[i] = u[i] - v[i]; - - // Return the norm of u-v - return _vector_norm(diff, p); + return _vector_norm(array_map(u, method({ v: v }, function(value, index) { + return value - self.v[index]; + })), p); } diff --git a/scripts/_vector_norm/_vector_norm.gml b/scripts/_vector_norm/_vector_norm.gml index 33df240..89ea83f 100644 --- a/scripts/_vector_norm/_vector_norm.gml +++ b/scripts/_vector_norm/_vector_norm.gml @@ -19,12 +19,9 @@ function _vector_norm(v) // Infinity-norm // Calculate the maximum value of |vi| - var maxval = -infinity; - for (var i = 0; i < array_length(v); i++) - { - if (abs(v[i]) > maxval) - maxval = abs(v[i]); - } + var maxval = array_reduce(v, function(previous, current, index) { + return max(previous, abs(current)); + }, -infinity); // Return the maximum return maxval; @@ -34,9 +31,9 @@ function _vector_norm(v) // Finite p-norm // Calculate the sum of |vi|^p - var tot = 0; - for (var i = 0; i < array_length(v); i++) - tot += power(abs(v[i]), p); + var tot = array_reduce(v, method({ p: p }, function(previous, current, index) { + return previous + power(abs(current), self.p); + })); // Return the 1/p power of the sum return power(tot, 1/p); diff --git a/scripts/_vector_scale/_vector_scale.gml b/scripts/_vector_scale/_vector_scale.gml index a0f5957..4f43d22 100644 --- a/scripts/_vector_scale/_vector_scale.gml +++ b/scripts/_vector_scale/_vector_scale.gml @@ -23,10 +23,7 @@ function _vector_scale(v, r) return v; // Otherwise rescale each component of the vector - var vr = array_create(array_length(v)); // output vector - for (var i = 0; i < array_length(v); i++) - vr[i] = v[i]*(r/ri); - - // Return the rescaled vector - return vr; + return array_map(v, method({ f: r / ri }, function(value, index) { + return value * self.f; + })); }