Skip to content

Commit 08325b5

Browse files
committed
Add _toArray, update _contains, _filter and _reduce
The updated functions now support ds_lists as well as arrays
1 parent 89c7f0e commit 08325b5

File tree

10 files changed

+142
-30
lines changed

10 files changed

+142
-30
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,21 @@ _concat(_arrayOf(0, 1, 2), _arrayOf(3, 4, 5));
129129

130130
### `_contains(collection, target, fromIndex = 0)`
131131

132-
Returns true if the given array contains the given value
132+
Returns true if the given collection contains the given value
133133

134134
```
135-
@param {String|Array|DS_Map} The collection to search
135+
@param {String|Array|DS_Map|DS_List} The collection to search
136136
@param {*} The value to look for
137137
@param {Real} [0] The index to begin looking from
138+
@param {ds_type} [ds_type_list] The type of the ds, if this is a ds.
138139
@returns {Boolean} True if the collection contains the target, otherwise false
139140
141+
Note: You only need to specify a ds_type when looking at a ds_list or ds_map.
142+
By default, the ds_type is set to ds_type_map. Options are:
143+
144+
* ds_type_list
145+
* ds_type_map
146+
140147
@example
141148
_contains([1, 2, 3], 1);
142149
// => true
@@ -164,12 +171,12 @@ _map(_filter(_collect(obj_enemy)), hasNoHealth), _destroy);
164171

165172
### `_filter(array, filterScript)`
166173

167-
Returns an array where values of the input array are truthy when run through the provided function.
174+
Returns a a collection where values of the input collection are truthy when run through the provided function.
168175

169176
```
170-
@param {Array} The array to filter
177+
@param {Array|DS_List} The collection to filter
171178
@param {Script} The script to filter with
172-
@returns {Array} The filtered array
179+
@returns {Array|DS_List} The filtered collection
173180
174181
@example
175182
_filter(_arrayOf(0, 1, 2, 3), lessThanTwo)
@@ -400,7 +407,7 @@ _or(false, false);
400407

401408
### `_partial(script, arg0, arg1 ... arg13)`
402409

403-
Creates a partial function identifier for use in place of raw scripts in gdash functions, or with the use of _run.
410+
Creates a partial function identifier for use in place of raw scripts in gdash functions, or with the use of `_run`.
404411

405412
Partials are to be treated as a data structure, and must be cleaned up with `_free()` when they are no longer of use.
406413

@@ -441,7 +448,7 @@ _push(_arrayOf(1, 2), 3);
441448
Reduces a collection by iterating over it with a function. Provided script should take 2 arguments: `total`, `value`. On the first call, `total` is `undefined`.
442449

443450
```
444-
@param {Array} The array to reduce
451+
@param {Array|DS_List} The collection to reduce
445452
@param {Script} The script to reduce with
446453
@returns {*} The reduced value from the given script
447454

src/gdash/gdash.yyp

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gdash/objects/obj_gdash_test_1/Other_14.gml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,14 @@ arr[4] = 5;
1010

1111
assert_equal(_reduce(arr, __sum), 15);
1212

13+
var list = ds_list_create();
14+
list[| 0] = 1;
15+
list[| 1] = 2;
16+
list[| 2] = 3;
17+
list[| 3] = 4;
18+
list[| 4] = 5;
19+
20+
assert_equal(_reduce(list, __sum), 15);
21+
1322
test_end();
1423

src/gdash/objects/obj_gdash_test_1/Other_19.gml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,24 @@ assert_is_false(_contains(res, 1));
1414
assert_is_false(_contains(res, 3));
1515
assert_is_false(_contains(res, 5));
1616

17+
var list = ds_list_create();
18+
ds_list_add(list, 0, 1, 2, 3, 4, 5, 6);
19+
var resList = _filter(list, __isEven);
20+
21+
_log(list[| 1]);
22+
_log(resList[| 1]);
23+
24+
assert_is_true(_contains(resList, 0, 0, ds_type_list));
25+
assert_is_true(_contains(resList, 2, 0, ds_type_list));
26+
assert_is_true(_contains(resList, 4, 0, ds_type_list));
27+
assert_is_true(_contains(resList, 6, 0, ds_type_list));
28+
29+
assert_is_false(_contains(resList, 1, 0, ds_type_list));
30+
assert_is_false(_contains(resList, 3, 0, ds_type_list));
31+
assert_is_false(_contains(resList, 5, 0, ds_type_list));
32+
33+
ds_list_destroy(list);
34+
ds_list_destroy(resList);
35+
1736
test_end();
1837

src/gdash/scripts/_contains/_contains.gml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// @desc Returns true if the given array contains the given value
2-
/// @param {String|Array|DS_Map} collection The collection to search
2+
/// @param {String|Array|DS_Map|DS_List} collection The collection to search
33
/// @param {*} value The value to look for
44
/// @param {Real} optionalFromIndex [0] The index to begin looking from
5+
/// @param {ds_type} optionalDSType [ds_type_list] The type of the ds, if this is a ds.
56
/// @returns {Boolean} True if the collection contains the target, otherwise false
67
/*
78
@example
@@ -19,10 +20,17 @@ var collection = argument[0];
1920
var target = argument[1];
2021
var fromIndex = 0;
2122

22-
if (argument_count == 3) {
23+
// TODO: This should default to ds_type_list in the next major version
24+
var dsType = ds_type_map;
25+
26+
if (argument_count > 2) {
2327
fromIndex = argument[2];
2428
}
2529

30+
if (argument_count > 3) {
31+
dsType = argument[3];
32+
}
33+
2634
if (is_string(collection)) {
2735

2836
// If it is a string, check to see if the target is in the string.
@@ -38,18 +46,32 @@ if (is_string(collection)) {
3846
}
3947
return false;
4048
} else if (is_real(collection)) {
41-
42-
// If it is a real, assume it is a ds ID.
43-
if (ds_exists(collection, ds_type_map)) {
49+
if (dsType == ds_type_map) {
4450
var keys = _keys(collection);
45-
for (var i = fromIndex; i < _length(keys); i++) {
46-
var thisValue = ds_map_find_value(collection, keys[i])
51+
var n = _length(keys);
52+
for (var i = fromIndex; i < n; i++) {
53+
var thisValue = collection[? keys[i]];
4754
if (_typeOf(thisValue) == _typeOf(target) && thisValue == target) {
4855
return true;
4956
}
5057
}
5158
return false;
59+
} else if (dsType == ds_type_list) {
60+
var n = _length(collection);
61+
for (var i = fromIndex; i < n; i++) {
62+
var thisValue = collection[| i];
63+
if (_typeOf(thisValue) == _typeOf(target) && thisValue == target) {
64+
return true;
65+
}
66+
}
67+
} else {
68+
show_error("Cannot look for value in ds type: " + string(dsType) + "\nIf using _contains with a data structure, it must be a list or map.", false);
5269
}
70+
} else if (is_undefined(collection)) {
71+
return false;
72+
} else {
73+
// Catch case for unknown collection
74+
show_error("Cannot look for value in type: " + _typeOf(collection) + "\nCollection must be a string, array, map or list.", false);
5375
}
54-
// Catch case for unknown collection
76+
5577
return false;

src/gdash/scripts/_filter/_filter.gml

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/// @desc Returns an array where values of the input array are truthy when run through the provided function.
2-
/// @param {Array} inputArray The array to filter
1+
/// @desc Returns a collection where values of the input collection are truthy when run through the provided function.
2+
/// @param {Array|ds_list} collection The collection to filter
33
/// @param {Script} filterScript The script to filter with
4-
/// @returns {Array} The filtered array
4+
/// @returns {Array|ds_list} The filtered collection
5+
/// Note: Returns the same type as the given collection. Creates a new list if the collection is a list.
6+
/// the original collection will not be destroyed, so if you pass in a list, you will still need to destroy it.
57
/*
68
@example
79
_filter(_arrayOf(0, 1, 2, 3), lessThanTwo)
@@ -10,14 +12,28 @@ _filter(_arrayOf(0, 1, 2, 3), lessThanTwo)
1012
*/
1113

1214
var result;
15+
var collection = argument0;
1316
var j = 0;
14-
var n = _length(argument0);
17+
var n = _length(collection);
18+
var type = _typeOf(collection);
1519

16-
for (var i = 0; i < n; i++) {
17-
if (_run(argument1, argument0[@ i])) {
18-
result[j] = argument0[@ i];
19-
j++;
20-
}
20+
if (type == "real") {
21+
result = ds_list_create();
22+
for (var i = 0; i < n; i++) {
23+
if (_run(argument1, collection[| i])) {
24+
result[| j] = collection[| i];
25+
j++;
26+
}
27+
}
28+
} else if (type == "array") {
29+
for (var i = 0; i < n; i++) {
30+
if (_run(argument1, collection[@ i])) {
31+
result[j] = collection[@ i];
32+
j++;
33+
}
34+
}
35+
} else {
36+
show_error("Cannot filter type: " + type + "\nCollection must be an array or ds_list", false);
2137
}
2238

2339
return result;

src/gdash/scripts/_reduce/_reduce.gml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// @desc Reduces a collection by iterating over it with a function. Provided script should take 2 arguments: total, value. On the first call, total is undefined.
2-
/// @param {Array} inputArray The array to reduce
2+
/// @param {Array|ds_list} collection The collection to reduce
33
/// @param {Script} recuderScript The script to reduce with
44
/// @returns {*} The reduced value from the given script
55
/*
@@ -13,13 +13,22 @@ _reduce(arr, concat);
1313
// => 'helloworld';
1414
*/
1515

16-
var array = argument[0];
16+
var collection = argument[0];
1717
var func = argument[1];
1818
var result = undefined;
19-
var n = _length(array);
19+
var n = _length(collection);
20+
var type = _typeOf(collection);
2021

21-
for (var i = 0; i < n; i++) {
22-
result = _run(func, result, array[@ i]);
22+
if (type == "real") {
23+
for (var i = 0; i < n; i++) {
24+
result = _run(func, result, collection[| i]);
25+
}
26+
} else if (type == "array") {
27+
for (var i = 0; i < n; i++) {
28+
result = _run(func, result, collection[@ i]);
29+
}
30+
} else {
31+
show_error("Cannot reduce type: " + type + "\nCollection must be an array or ds_list", false);
2332
}
2433

2534
return result;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// @desc Converts the given ds_list to an array
2+
/// @param list
3+
4+
var list = argument0;
5+
var listSize = ds_list_size(list);
6+
var array;
7+
8+
for (var i = listSize - 1; i >= 0; i--) {
9+
array[i] = list[| i];
10+
}
11+
12+
return array;

src/gdash/scripts/_toArray/_toArray.yy

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gdash/views/8ebd4763-7203-42aa-9554-cc55d03a802b.yy

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)