Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array_max_ext (and similar for other array functions) #102

Open
Appsurdgames opened this issue Dec 7, 2024 · 3 comments
Open

array_max_ext (and similar for other array functions) #102

Appsurdgames opened this issue Dec 7, 2024 · 3 comments
Labels
feature ✨ For feature requests and implementations

Comments

@Appsurdgames
Copy link

Appsurdgames commented Dec 7, 2024

The function array_max (already implemented) returns the maximum value in the array.
It would make sense to offer array_max_ext which does the exact same thing,
but returns an array containing the maximum value as first entry and the index of this value as second entry.

A similar extension is possible for array_min, array_mean, array_sum and array_median.

Here is a possible implementation:

/// @param {array}	array	Input array
/// @return [max_value, index]

function array_max_ext(_array)
{
	if is_array(_array)
	{
		max_val = -infinity;
		index = -1;
		for(var i = 0; i<array_length(_array); i++)
		{
			if (_array[i] > max_val)
			{
				max_val = _array[i];
				index = -1;
			}
		}
		return [max_val, index];
	}
	else
	{
		return [undefined, undefined];
	}
}```
@Alphish
Copy link
Owner

Alphish commented Dec 7, 2024

I'd be wary of returning a new array for each call, as it might clutter lots of memory pretty quick, depending on the usage.

However, I wouldn't mind a function like array_max_index and its kin, which would return the first index of the max value, and could then be used to quickly retrieve the actual max value. If someone's so inclined, they could make the array_max_ext function like yours using array_max_index then.

@tabularelf
Copy link

tabularelf commented Dec 12, 2024

I think I too prefer an array_max_index if anything.
I actually wonder how fast it'd be if we cobble it with array_max + array_get_index. 🤔
(Though a loop makes more logical sense IMO, just food for thought.)

@Appsurdgames
Copy link
Author

Appsurdgames commented Dec 13, 2024

I forgot about memory, thanks for bring that up. Then a version just returning the index would be awesome :)

@Alphish Alphish added the feature ✨ For feature requests and implementations label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ✨ For feature requests and implementations
Projects
None yet
Development

No branches or pull requests

3 participants