The function DuplicateFreeList
in GAP, which is the same a the function Unique
, takes a list and returns all the elements of that list with duplicates removed. My problem is that this function checks for duplicates using =
, whereas I want to remove duplicates in a list up to isomorphism. Is there a GAP function like DuplicateFreeList
that removes duplicates using an arbitrary binary true/false function? Or if not is there a slick way to write a wrapper around DuplicateFreeList
which modifies the =
operator?
Asked
Active
Viewed 110 times
3

Mike Pierce
- 18,938
1 Answers
3
There is no such function, but given how pathetic the actual code for Unique
is, it is not hard to write such a function:
UniqueByFunction:=function ( list,fun )
local l,i;
l:= [];
for i in list do
if ForAll(l,x->fun(x,i)=false) then
Add(l,i);
fi;
od;
return l;
end;
(takes a list and an equality test function that returns true
or false
when given two objects as arguments. That is UniqueByFunction(list,\=);
has the same effect as Unique(list);
.)
Caveat: On a list of length $n$ you will be doing $n(n-1)$ isomorphism tests, each of which often is expensive. In practice you thus most often want to partition the list according to suitable isomorphism invariants first and then run the tests only within each cell.

ahulpke
- 18,416
- 1
- 21
- 38