Identical Items List
Introduction
The package identical_items_list
provides
a non-empty unmodifiable Dart list containing identical
items. The list is unmodifiable in the sense that object-mutating methods
are not implemented and throw an UnsupportedError
.
Use Case
Consider a function that returns a (potentially very long) list which
for certain conditions contains identical entries. In such cases,
instead of creating and returning a standard List
object,
it may be more efficient to return an
IdenticalItemsList
since
it implements the interface
List<E>
without using an underlying collection.
Usage
To use this library include identical_items_list
as a dependency in your pubspec.yaml file. The
example below shows how to construct an object of type
IdenticalItemsList
.
Note: It is not possible to create an
empty IdenticalItemsList
. If a non-positive constructor
parameter length
is provided, the value 1 will be used instead.
import 'package:identical_items_list/identical_items_list.dart';
void main(List<String> args) {
final list = IdenticalItemsList(value: 42, length: 1000000);
print('List: $list \n');
print('Type: list is List<int>: ${list is List<int>} \n');
print('Length: ${list.length} \n');
final sum = list.reduce((previousValue, item) => previousValue + item);
print('Sum: $sum \n');
print('Access: list[1024] = ${list[1024]}');
}
Click to show the console output.
$ dart example/bin/example.dart
List: [42, 42, 42, 42, 42, ..., 42, 42]
Type: is List<int>: true
Length: 1000000
Sum: 42000000
Access: list[1024] = 42
Note: The class IdenticalItemsList
provides a const
constructor making it possible to define const objects:
final list1 = const IdenticalItemsList(value: 42, length: 1000);
final list2 = const IdenticalItemsList(value: 42, length: 1000);
print(list1 == list2); // true
print(identical(list1 == list2)); // true
Examples
For further information see example.
Features and bugs
Please file feature requests and bugs at the issue tracker.