unique method
Returns a new lazy Iterable with unique elements from this collection.
If comparator is provided, it uses the custom logic to determine
if two elements are equal. If comparator is null, it falls back to
the default equality operator (==).
The comparator should return true if a and b are considered duplicates.
Implementation
Iterable<E> unique([bool Function(E a, E b)? comparator]) {
final List<E> result = [];
for (final element in this) {
if (comparator == null) {
// Fallback to standard equality and hashCode check
if (!result.contains(element)) {
result.add(element);
}
} else {
// Use custom comparison logic
bool isDuplicate = false;
for (final existing in result) {
if (comparator(existing, element)) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
result.add(element);
}
}
}
return result;
}