Last active
December 20, 2015 17:39
-
-
Save choplin/6170637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE OR REPLACE FUNCTION eval(_ anyelement, proc Text) RETURNS anyelement AS $$ | |
| BEGIN | |
| EXECUTE 'SELECT ' || replace(proc, '_', _::text) INTO $0; | |
| RETURN $0; | |
| END; | |
| $$ LANGUAGE plpgsql; | |
| CREATE OR REPLACE FUNCTION map(ary anyarray, proc Text) RETURNS anyarray AS $SQL$ | |
| SELECT array_agg(eval(_, proc)) | |
| FROM unnest(ary) AS _ | |
| $SQL$ LANGUAGE sql; | |
| select map(ARRAY[1,2,3]::int[], '_ + 1'); | |
| map | |
| --------- | |
| {2,3,4} | |
| (1 row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment