mirror of
https://github.com/hanskokx/arcane_helper_utils.git
synced 2026-05-14 10:29:07 +02:00
Update docs for unique
Signed-off-by: Hans Kokx <hans.kokx@hackberry.se>
This commit is contained in:
@@ -3,21 +3,6 @@
|
||||
/// This extension provides a `unique` method that removes duplicate entries
|
||||
/// from the list, optionally using a custom identifier function.
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// final list = [1, 2, 2, 3, 4, 4];
|
||||
/// final uniqueList = list.unique();
|
||||
/// print(uniqueList); // Output: [1, 2, 3, 4]
|
||||
///
|
||||
/// final people = [
|
||||
/// Person(id: 1, name: 'Alice'),
|
||||
/// Person(id: 2, name: 'Bob'),
|
||||
/// Person(id: 1, name: 'Alice Duplicate'),
|
||||
/// ];
|
||||
/// final uniquePeople = people.unique((person) => person.id);
|
||||
/// print(uniquePeople.map((p) => p.name)); // Output: ['Alice', 'Bob']
|
||||
/// ```
|
||||
///
|
||||
/// The method also supports in-place filtering for efficient memory usage.
|
||||
extension Unique<E, Id> on List<E> {
|
||||
/// Returns a new list with unique elements, filtered by the provided [id] function.
|
||||
@@ -36,6 +21,22 @@ extension Unique<E, Id> on List<E> {
|
||||
/// Returns:
|
||||
/// A `List<E>` containing unique elements based on the specified [id] or the elements
|
||||
/// themselves if no [id] function is provided.
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// final list = [1, 2, 2, 3, 4, 4];
|
||||
/// final uniqueList = list.unique();
|
||||
/// print(uniqueList); // Output: [1, 2, 3, 4]
|
||||
///
|
||||
/// final people = [
|
||||
/// Person(id: 1, name: 'Alice'),
|
||||
/// Person(id: 2, name: 'Bob'),
|
||||
/// Person(id: 1, name: 'Alice Duplicate'),
|
||||
/// ];
|
||||
/// final uniquePeople = people.unique((person) => person.id);
|
||||
/// print(uniquePeople.map((p) => p.name)); // Output: ['Alice', 'Bob']
|
||||
/// ```
|
||||
///
|
||||
List<E> unique([Id Function(E element)? id, bool inplace = true]) {
|
||||
final Set ids = {};
|
||||
final List<E> list = inplace ? this : List<E>.from(this);
|
||||
|
||||
Reference in New Issue
Block a user