Proposals
A function enabling one to remove an element by offset from an array.
Offsets in PostgreSQL start at 1 rather than 0 like most other implementations.
This concept should also be adopted by anyarray
, as itself is an
implementation specific to PostgreSQL.
Perform function overloading, where an optional third parameter is present signaling that the second argument is an offset rather than a value.
Function Name
anyarray_remove
Arguments:
-
anyarray
: The array from which to remove the element from. -
integer
or `integer[]: The offset(s) at which the element(s) to be removed reside(s). Any offset(s) passed which does not exist is simply ignored. -
boolean
: PassTRUE
to informanyarray_remove
that the second argument is an offset, rather than a value.
Returns:
anyarray
Calling:
SELECT ANYARRAY_REMOVE(ARRAY[9,8,7], 2, TRUE); -- Returns: {9,7} SELECT ANYARRAY_REMOVE(ARRAY[9,8,7], 4, TRUE); -- Returns: {9,8,7} SELECT ANYARRAY_REMOVE(ARRAY[9,8,7], ARRAY[1,2,4], TRUE); -- Returns: {7}
Dedicate a function specifically to removing an element by offset.
Function Name
anyarray_offset_remove
Arguments:
-
anyarray
: The array from which to remove the element from. -
integer
or `integer[]: The offset(s) at which the element(s) to be removed reside(s). Any offset(s) passed which does not exist is simply ignored.
Returns:
anyarray
Calling:
SELECT ANYARRAY_OFFSET_REMOVE(ARRAY[9,8,7], 2); -- Returns: {9,7} SELECT ANYARRAY_OFFSET_REMOVE(ARRAY[9,8,7], 4); -- Returns: {9,8,7} SELECT ANYARRAY_OFFSET_REMOVE(ARRAY[9,8,7], ARRAY[1,2,4]); -- Returns: {7}
A function which maps the specific function and arguments to every element within an array.
This function would support an undetermined number of arguments in arbitrary order to facilitate the mapping of any function possible.
Stick to the current anyarray standard, where the first argument is always the array with which the action is being performed on.
Function Name
anyarray_map
Arguments:
Argument #1: anyarray
: The array of elements which will have the function called on.
Argument #2: text
: The function to be called. $1 represents the element from the array. $2, $3, etc. represent additional arguments to be passed.
Argument #3, #4, #...: Additional arguments to be passed to the function being mapped to.
Returns:
anyarray
Calling:
SELECT ANYARRAY_MAP(ARRAY[' a ',' b','c'], 'TRIM($1)'); -- Returns: {a,b,c} -- Equivalent to: -- TRIM(' a ') -- TRIM(' b') -- TRIM('c') SELECT ANYARRAY_MAP(ARRAY['hi','howdy'], 'REPLACE($1, $2, $3)', 'hi', 'hello'); -- Equivalent to: -- REPLACE('hi', 'hi', 'hello') -- REPLACE('howdy', 'hi', 'hello') -- Returns: {hello,howdy}
It may be easier to understand what a call to array_map
is doing (and perhaps
easier to define a call as well,) if the function call is passed first, followed
by the subject array. All additional arguments will be passed subsequently.
Function Name
anyarray_map
Arguments:
Argument #1: text
: The function to be called. $1 represents the element from the array. $2, $3, etc. represent additional arguments to be passed.
Argument #2: anyarray
: The array of elements which will have the function called on.
Argument #3, #4, #...: Additional arguments to be passed to the function being mapped to.
Returns:
anyarray
Calling:
SELECT ANYARRAY_MAP('TRIM($1)', ARRAY[' a ',' b','c']); -- Returns: {a,b,c} -- Equivalent to: -- TRIM(' a ') -- TRIM(' b') -- TRIM('c') SELECT ANYARRAY_MAP('REPLACE($1, $2, $3)', ARRAY['hi','howdy'], 'hi', 'hello'); -- Equivalent to: -- REPLACE('hi', 'hi', 'hello') -- REPLACE('howdy', 'hi', 'hello') -- Returns: {hello,howdy}