mirror of
https://github.com/sfall-team/sfall.git
synced 2026-07-27 16:52:34 -07:00
Updated headers
- Add a bunch of array-related procs to unlock basic functional programming - Fix error with FUNC_SELECTOR_7
This commit is contained in:
@@ -35,6 +35,9 @@ procedure array_keys(variable array);
|
|||||||
// list of array values (useful for maps)
|
// list of array values (useful for maps)
|
||||||
procedure array_values(variable array);
|
procedure array_values(variable array);
|
||||||
|
|
||||||
|
// makes given array permanent and returns it
|
||||||
|
procedure array_fixed(variable array);
|
||||||
|
|
||||||
// returns temp array containing a subarray starting from $index with $count elements
|
// returns temp array containing a subarray starting from $index with $count elements
|
||||||
// negative $index means index from the end of array
|
// negative $index means index from the end of array
|
||||||
// negative $count means leave this many elements from the end of array
|
// negative $count means leave this many elements from the end of array
|
||||||
@@ -91,6 +94,11 @@ procedure get_empty_array_index(variable array);
|
|||||||
procedure add_array_set(variable array, variable item);
|
procedure add_array_set(variable array, variable item);
|
||||||
procedure remove_array_set(variable array, variable item);
|
procedure remove_array_set(variable array, variable item);
|
||||||
|
|
||||||
|
// Creates a new array filled from a given array by transforming each value using given procedure name.
|
||||||
|
procedure array_transform(variable arr, variable valueFunc);
|
||||||
|
|
||||||
|
// Create a new temp array filled from a given array by transforming each key and value using given procedure names.
|
||||||
|
procedure array_transform_kv(variable arr, variable keyFunc, variable valueFunc);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -223,6 +231,11 @@ procedure array_values(variable array) begin
|
|||||||
return tmp;
|
return tmp;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
procedure array_fixed(variable array) begin
|
||||||
|
fix_array(array);
|
||||||
|
return array;
|
||||||
|
end
|
||||||
|
|
||||||
procedure array_slice(variable array, variable index, variable count) begin
|
procedure array_slice(variable array, variable index, variable count) begin
|
||||||
variable tmp, i, n;
|
variable tmp, i, n;
|
||||||
n := len_array(array);
|
n := len_array(array);
|
||||||
@@ -401,13 +414,25 @@ procedure remove_array_set(variable array, variable item) begin
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
// Transform every value in array using given callback
|
// Creates a new array filled from a given array by transforming each value using given procedure name.
|
||||||
procedure array_map_func(variable arr, variable callback) begin
|
procedure array_transform(variable arr, variable valueFunc) begin
|
||||||
variable k, v;
|
variable k, v, retArr := temp_array_map if array_is_map(arr) else temp_array(len_array(arr), 0);
|
||||||
foreach (k: v in arr)
|
foreach (k: v in arr) begin
|
||||||
arr[k] := callback(v);
|
retArr[k] := valueFunc(v);
|
||||||
return arr;
|
|
||||||
end
|
end
|
||||||
|
return retArr;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
// Create a new temp array filled from a given array by transforming each key and value using given procedure names.
|
||||||
|
procedure array_transform_kv(variable arr, variable keyFunc, variable valueFunc) begin
|
||||||
|
variable k, v, retArr := temp_array_map;
|
||||||
|
foreach (k: v in arr) begin
|
||||||
|
retArr[keyFunc(k)] := valueFunc(v);
|
||||||
|
end
|
||||||
|
return retArr;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
#define ARRAY_EMPTY_INDEX (-1)
|
#define ARRAY_EMPTY_INDEX (-1)
|
||||||
|
|
||||||
@@ -586,30 +611,45 @@ end
|
|||||||
Different utility functions...
|
Different utility functions...
|
||||||
*/
|
*/
|
||||||
|
|
||||||
procedure debug_array_str(variable arr) begin
|
#define debug_array_str(arr) debug_array_str_deep(arr, 1)
|
||||||
variable i := 0, k, s, len;
|
|
||||||
|
procedure debug_array_str_deep(variable arr, variable levels) begin
|
||||||
|
#define _newline if (levels > 1) then s += "\n";
|
||||||
|
#define _indent string_repeat(" ", levels - 1)
|
||||||
|
#define _value(v) (v if (levels <= 1 or not array_exists(v)) else debug_array_str_deep(v, levels - 1))
|
||||||
|
variable i := 0, k, v, s, len;
|
||||||
len := len_array(arr);
|
len := len_array(arr);
|
||||||
if (array_is_map(arr)) then begin // print assoc array
|
if (array_is_map(arr)) then begin // print assoc array
|
||||||
s := "Map("+len+"): {";
|
s := "Map("+len+"): {";
|
||||||
while i < len do begin
|
while i < len do begin
|
||||||
|
_newline
|
||||||
k := array_key(arr, i);
|
k := array_key(arr, i);
|
||||||
s += k + ": "+ get_array(arr, k);
|
v := get_array(arr, k);
|
||||||
|
s += _indent + k + ": " + _value(v);
|
||||||
if i < (len - 1) then s += ", ";
|
if i < (len - 1) then s += ", ";
|
||||||
i++;
|
i++;
|
||||||
end
|
end
|
||||||
if (strlen(s) > 254) then s := substr(s, 0, 251) + "...";
|
//if (strlen(s) > 254) then s := substr(s, 0, 251) + "...";
|
||||||
|
_newline
|
||||||
s += "}";
|
s += "}";
|
||||||
end else begin // print list
|
end else begin // print list
|
||||||
s := "List("+len+"): [";
|
s := "List("+len+"): [";
|
||||||
|
_newline
|
||||||
while i < len do begin
|
while i < len do begin
|
||||||
s += arr[i];
|
_newline
|
||||||
|
v := get_array(arr, i);
|
||||||
|
s += _indent + _value(v);
|
||||||
if i < (len - 1) then s += ", ";
|
if i < (len - 1) then s += ", ";
|
||||||
i++;
|
i++;
|
||||||
end
|
end
|
||||||
if (strlen(s) > 254) then s := substr(s, 0, 251) + "...";
|
//if (strlen(s) > 254) then s := substr(s, 0, 251) + "...";
|
||||||
|
_newline
|
||||||
s += "]";
|
s += "]";
|
||||||
end
|
end
|
||||||
return s;
|
return s;
|
||||||
|
#undef _newline
|
||||||
|
#undef _indent
|
||||||
|
#undef _value
|
||||||
end
|
end
|
||||||
|
|
||||||
procedure _PURGE_all_saved_arrays begin
|
procedure _PURGE_all_saved_arrays begin
|
||||||
|
|||||||
@@ -129,6 +129,21 @@ procedure string_split_ints(variable str, variable split) begin
|
|||||||
return result;
|
return result;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
// atoi proc wrapper, for use as callback
|
||||||
|
procedure string_to_int(variable str) begin
|
||||||
|
return atoi(str);
|
||||||
|
end
|
||||||
|
|
||||||
|
// atof proc wrapper, for use as callback
|
||||||
|
procedure string_to_float(variable str) begin
|
||||||
|
return atof(str);
|
||||||
|
end
|
||||||
|
|
||||||
|
// converts any value to a string, for use as callback
|
||||||
|
procedure to_string(variable val) begin
|
||||||
|
return ""+val;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
DEPRECATED, use string_format instead!
|
DEPRECATED, use string_format instead!
|
||||||
|
|||||||
@@ -433,6 +433,4 @@
|
|||||||
#define get_current_quick_save_slot metarule3(213, 0, 0, 0)
|
#define get_current_quick_save_slot metarule3(213, 0, 0, 0)
|
||||||
#define set_current_quick_save_slot(page, slot, check) metarule3(214, page, slot, check) // check: 1 - don't check slot when saving
|
#define set_current_quick_save_slot(page, slot, check) metarule3(214, page, slot, check) // check: 1 - don't check slot when saving
|
||||||
|
|
||||||
#undef FUNC_SELECTOR_7
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user