Skip to content

Commit

Permalink
.join() method
Browse files Browse the repository at this point in the history
added join and push methods (push is just a copy of add and append)
  • Loading branch information
evolutionleo committed Aug 30, 2020
1 parent 50f4844 commit 7748b24
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 22 deletions.
55 changes: 44 additions & 11 deletions ArrayClass.gml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ function Array() constructor {
return self;
}

///@function join(separator)
///@description returns a string, containing all of the array values separated by 'sep'
///@tip to join part of the array, use array.slice().join()
///@param {string} separator
///@param {bool} show_bounds
static join = function(sep, show_bounds) {
if is_undefined(sep)
sep = ", "
if is_undefined(show_bounds)
show_bounds = true

_sep = sep

if show_bounds
str = "["
else
str = ""

forEach(function(el, i) {
str += string(el)
if(i < size-1)
str += _sep
})

if show_bounds
str += "]"

return str
}

///@function lambda(func)
///@description Loops through the array and applies the function to each element
///@param {function} func(x, *pos)
Expand Down Expand Up @@ -393,6 +423,19 @@ function Array() constructor {
return ans;
}

///@function push(value, value2, ..)
///@description Mirrors append() method
///@param {any} value
static push = function(value) {
for(var i = 0; i < argument_count; ++i) {
var val = argument[i]
content[size] = val;
++size;
}

return self;
}

///@function pushBack(value)
///@description inserts a value to the beginning of the array
///@param {any} value
Expand Down Expand Up @@ -616,17 +659,7 @@ function Array() constructor {


static toString = function() {
str = "[";

forEach(function(el, i) {
str += string(el);
if(i < size-1)
str += ", ";
});

str += "]";

return str;
return self.join()
}
}

Expand Down
Binary file modified ArrayClass.yymps
Binary file not shown.
55 changes: 44 additions & 11 deletions Sample+Tests/scripts/ArrayClass/ArrayClass.gml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ function Array() constructor {
return self;
}

///@function join(separator)
///@description returns a string, containing all of the array values separated by 'sep'
///@tip to join part of the array, use array.slice().join()
///@param {string} separator
///@param {bool} show_bounds
static join = function(sep, show_bounds) {
if is_undefined(sep)
sep = ", "
if is_undefined(show_bounds)
show_bounds = true

_sep = sep

if show_bounds
str = "["
else
str = ""

forEach(function(el, i) {
str += string(el)
if(i < size-1)
str += _sep
})

if show_bounds
str += "]"

return str
}

///@function lambda(func)
///@description Loops through the array and applies the function to each element
///@param {function} func(x, *pos)
Expand Down Expand Up @@ -393,6 +423,19 @@ function Array() constructor {
return ans;
}

///@function push(value, value2, ..)
///@description Mirrors append() method
///@param {any} value
static push = function(value) {
for(var i = 0; i < argument_count; ++i) {
var val = argument[i]
content[size] = val;
++size;
}

return self;
}

///@function pushBack(value)
///@description inserts a value to the beginning of the array
///@param {any} value
Expand Down Expand Up @@ -616,17 +659,7 @@ function Array() constructor {


static toString = function() {
str = "[";

forEach(function(el, i) {
str += string(el);
if(i < size-1)
str += ", ";
});

str += "]";

return str;
return self.join()
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sample+Tests/scripts/Tests/Tests.gml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ function __Array_test() {
show_debug_message("Sliced 0-4. Result: "+string(array_slice))


// Joining
show_debug_message("#### PRINTING (JOINING) ####")
show_debug_message(array.join("|"))


// sorting (slow, don't do every frame)
Expand Down

0 comments on commit 7748b24

Please sign in to comment.