array extended functions
Posted: Wed Mar 27, 2013 7:36 am
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.
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.