Skip to content

Commit

Permalink
doc: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Aug 5, 2024
1 parent 743ca70 commit cab7716
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
74 changes: 65 additions & 9 deletions book/src/libs/array/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,71 @@
***
`Arr` (Array) is a zero cost extension type of `List`, where the `List` is treated as non-growable. This is useful for correctly handling lists where growable is false and const lists - as these types of lists are treated the same in the regular Dart type system, which may lead to errors. With `Arr`, type intent is clear for maintainers and developers are able think about code performance more critically.
```dart
var array = Arr(null, 10);
array = Arr.constant(const [1,2,3,4,5]);
array = Arr.generate(10, (i) => i);
array = Arr.range(0, 10, step: 2);
for(final entry in array){
// do something
Arr<int?> array = Arr(null, 10);
```
`Arr`'s allocation will be more efficient than compared to a `List` since it does not reserve additional capacity and allocates the full amount eagerly. Which is important since allocations account for most of the cost of the runtime costs of a List.

## Examples

### Creating Arrays

You can create `Arr` instances in several ways:

#### From a Default Value

```dart
var array = Arr(null, 10); // Creates an array of 10 null values
```

#### From a Constant List

```dart
var array = Arr.constant(const [1, 2, 3, 4, 5]); // Creates an array from a constant list
```

#### Using a Generator Function

```dart
var array = Arr.generate(10, (i) => i); // Generates an array with values from 0 to 9
```

#### Creating a Range

```dart
var array = Arr.range(0, 10, step: 2); // Creates an array with values [0, 2, 4, 6, 8]
```

### Accessing Elements

You can access and modify elements just like a normal list:

```dart
var entry = array[2]; // Accesses the element at index 2
array[2] = 10; // Sets the element at index 2 to 10
```

### Iterating Over Elements

You can use a for-in loop to iterate over the elements:

```dart
for (final entry in array) {
print(entry); // Do something with each entry
}
var entry = array[2];
```

### Converting to List

You can convert `Arr` back to a regular list (this will be erased at compile time so there is no cost):

```dart
var list = array.list;
var (slice1, slice2) = array.splitSlice(3);
```
`Arr`'s allocation will be more efficient than compared to a `List` since it does not reserve additional capacity and allocates the full amount eagerly. Which is important since allocations account for most of the cost of the runtime costs of a List.

### Splitting Arrays

You can split an array into two slices:

```dart
var (slice1, slice2) = array.splitSlice(3); // Splits the array at index 3
```
2 changes: 1 addition & 1 deletion book/src/libs/cell/cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ final secondCall = await lazyCell.force();
expect(callCount, equals(1));
expect(secondCall, equals(20));
```
The base type for all `LazyCell`s is `NullableLazyCell`.
The base type for all `AsyncLazyCell`s is `AsyncNullableLazyCell`.

[pub]:https://pub.dev/documentation/rust_core/latest/cell/cell-library.html

0 comments on commit cab7716

Please sign in to comment.