array extended functions

Made a small change or new addition to the POL Core that makes a difference? You can post the changes here in .patch or .diff file format, for our Dev team to screen and apply to the SVN!

Moderator: POL Developer

Locked
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

array extended functions

Post by andenixa »

The purpose of the patch is to extend the existing array methods API.

Added functions:

array.all() - return 1 if all elements of the array are true, 0 otherwise. Example: {1, "hello"}.all() -> 1, {0, "hello"} -> 0 // because 0 is false
array.any() - return 1 if any element of the array is true, 0 otherwise. Example: {0, 1, "hi"}.any() -> 1, {0, ""} -> 0 // because 0 and "" are both false
array.remove(value) - removes any object found in the array, returns 1 if the object is found, 0 on failure. Example: {0, 1, "hi"}.remove(1) -> 1, the resulting array is {0, "hi"}
array.removeall(value) - same as above, but removes all the occurrences of value found inside the array
array.pop() - removes the last element of the array, returning it. Example: {1, 2, 3}.pop() -> 3, when the array shrinks by one element to {1,2}
array.poprandom() - same as above, but retrieves a random element of the array removing it from the array afterwards.
array.sortby(membername) - sorts an array of objects by their member value, ascending order. Example: {attacker,
defender}.sortby("cmdlevel") will sort mobiles by their .cmdlevel
array.join(sep) - creates a concatenated (joined) string representation of the array contents, joined by sep. Example: {"I", "am",
"a", "warrior", "level", 1}.join(" ") -> "I am a warrior level 1"
array.collect(membername, [membervalue]) - creates new array that includes all element of this array with membername equals membervalue. If membervalue isn't specified then new array will have all the elements for which .membername returns true. Note: this method will *always* remove all uninit objects from the array. Ex: {who, "test"}.collect("name") -> {who} // doesn't include string "name" because it doesn't have .name member.
array.reject(membername, [membervalue]) - creates new array that includes all element of an old array whose membername not equals to membervalue. If membervalue isn't specified then new array will have all the elements for which .membername returns false or non-existent.




PS: Kevin had counseled me on POL intrinsic mechanics throughout the development process. Without him the API would not have been possible.
Attachments
array_extended_api_v2.patch
POL Array additional methods
(10.47 KiB) Downloaded 292 times
Last edited by andenixa on Sun Apr 14, 2013 3:07 am, edited 3 times in total.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: array extended functions

Post by Yukiko »

Is there any error checking with array.sortby() for non-UO objects that might be stored in an array element and array.join() for non-strings stored in an array element?
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: array extended functions

Post by andenixa »

Hello Yukiko,
As of now objects with inexistent members are being resolved as "uninit". They don't cause any issues, but give somewhat random sorting results.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: array extended functions

Post by Yukiko »

I don't know if it's possible but it might be good to provide error checking for those methods that require specific datatypes in arrays. That way your methods would be consistent with POL's normal operation. As a general rule POL does provide error checking when there are incompatible datatypes. It definitely helps with debugging code if a method/function returns error information.

Anyway, thanks.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: array extended functions

Post by andenixa »

There is an extensive error checking for all the calls that require a specific data type in the array. Just for sorting I don't think its due. Besides I am not sure that the team would ever consider my API anyway.
Turley
POL Developer
Posts: 670
Joined: Sun Feb 05, 2006 4:45 am

Re: array extended functions

Post by Turley »

Why should we not? For me it sounds like a good addition, I am planning to look into it but hadn't the time to do so.
Give me some time :)
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: array extended functions

Post by Yukiko »

At first glance some of the methods do appear very specialized but I can see the application for others. So I don't see why the developers wouldn't add your patch.
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: array extended functions

Post by andenixa »

I'd also like to hear if you have any comments about my string.format() call. I consider it to have even better potential. At least at the shard I use it quite frequently. I'd gladly patch any instabilities had they arise.
Locked