5.113 Remove duplicates objects in a list based on condition on a field

I had case where there is list of Objects, and wanted to removed duplicate entries in the list based on if some field is the same among the objects.

This can be done using the command ListTools:-MakeUnique and using a proc which checks for the condition. In this example, we want to remove objects where the field age in each object is the same.

restart; 
 
#create data type 
module person_type() 
    option object; 
    export name::string:="me"; 
    export age:=25; 
end module: 
 
#make few objects 
o1:=Object(person_type); 
o2:=Object(person_type); 
o3:=Object(person_type); 
o3:-age:=46; 
o4:=Object(person_type); 
 
#make list of them 
list_of_people:=[o1,o2,o3,o4]; 
 
nops(list_of_people);  #this will print 4 
 
#now delete object if age is same 
list_of_people:=ListTools:-MakeUnique( 
            list_of_people, 
            1, 
            proc(a,b) 
                evalb(a:-age=b:-age); 
            end proc 
        ); 
 
nops(list_of_people); #this will print 2