Here’s a solution in pure SQL, for reference: CREATE OR REPLACE FUNCTION public.pairwise( cards card[] ) RETURNS table(c1 card, c2 card) LANGUAGE sql AS $function$ with individual_cards as ( select * from unnest(cards) with ordinality c ) select c(c1.suit, c1.rank), c(c2.suit, c2.rank) from individual_cards c1 join individual_cards c2 on c1.ordinality = c2.ordinality - 1 where c1.ordinality % 2 = 1 $function$ ; Given that Postgres often (with good cause) touts its type system, it’s a shame that this basic structured type is great in many ways, but seriously flawed in really simple ones. |