Skip to content

Instantly share code, notes, and snippets.

@choplin
Last active December 20, 2015 17:39
Show Gist options
  • Select an option

  • Save choplin/6170637 to your computer and use it in GitHub Desktop.

Select an option

Save choplin/6170637 to your computer and use it in GitHub Desktop.
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