collection_ext 0.2.0

collection_ext #

Pub Check Status Code Coverage MIT

A set of extension methods for Dart collections, designed for the purpose of making it easier to write concise, functional-programming-styled Dart code.

Working on an Iterable for example, with collection_ext, we can write:

iterable.forEachIndexed((i, x) => /* use index i & element x */)

instead of:

var i = 0;
for (var x in iterable) {
  // use index i & element x
  i++;
}

Usage #

Import all extension methods at once:

import 'package:collection_ext/all.dart';

Column(
  children: getItems()
    .nonNull
    .mapIndexed((i, item) => Text("#$i ${item.title}"))
    .asList(),
)

Or you can import the needed module only, for example:

import 'package:collection_ext/iterables.dart';

final diff = [2, 4, 6].foldRight(0, (x, acc) => x - acc);

Available Modules #

👉 See API Docs for more details

I'm working on more useful extensions, PRs are welcome! 🍻🖖

👉 Release History

example/main.dart

import 'package:collection_ext/currying.dart';
import 'package:collection_ext/iterables.dart';
import 'package:collection_ext/maps.dart';
import 'package:collection_ext/numbers.dart';
import 'package:collection_ext/ranges.dart';

void main() {
  iterablesExample();
  mapsExample();
  rangesExample();
  curryingExample();
}

/// Example usage of extensions to [Iterable]s.
void iterablesExample() {
  // tests if all the numbers are even
  final isEven = [5, 19, 2].all((n) => n.isEven);
  print('[5, 19, 2] are all even? $isEven');

  // index-aware `forEach`
  var indexes = '';
  [5, 6, 7].forEachIndexed((i, n) => indexes += '$i');
  print('indexes walked through: $indexes'); // => '012'

  // `fold` the list in reverse direction
  final subRight =
      [1, 2, 3, 4].foldRight(0, subtract); // => (1 - (2 - (3 - (4 - 0)))) => -2
  final subLeft =
      [1, 2, 3, 4].fold(0, subtract); // => ((((0 - 1) - 2) - 3) - 4) => -10
  print(
      '[1, 2, 3, 4].foldRight(0, subtract) => $subRight, while `fold` => $subLeft');

  // duplicates each element using `flatMap`
  final duplicated = [1, 2, 3].flatMap((n) => [n, n]); // [1, 1, 2, 2, 3, 3]
  print('[1, 2, 3].flatMap((n) => [n, n]) => $duplicated');

  // splits the numbers into negatives and non-negatives
  final parts =
      [1, 0, -3, 4, -6].partition((n) => n.isNegative); // ([-3, -6], [1, 0, 4])
  print('split [1, 2, -3, 4, -6] by sign => $parts');

  // chunks a list into subsets with maximum length of 2
  final chunks = [1, 0, -3, 4, -6].chunked(2); // [[1, 0], [-3, 4], [-6]]
  print('chunks [1, 2, -3, 4, -6] by length of 2 => $chunks');
}

/// Example usage of extensions to [Map]s.
void mapsExample() {
  // turns a sequence of key/value pairs (a key followed by a value) into a map
  final map = mapFromList<int, String>([10, 'ten', -3, 'minus three']);
  print('map built from a list => $map');

  // tests if all the numbers are even
  final isEven = {'a': 5, 'b': 19, 'c': 2}.all((k, v) => v.isEven);
  print('the map contains only even numbers? $isEven');

  // transforms the map entries into another iterable
  final medals = {1: 'Gold', 2: 'Silver', 3: 'Bronze'}
      .mapEntries((k, v) => '$v is the #$k');
  print('ranking of medal: $medals');

  // duplicates entries using `flatMap`
  final duplicated = {1: 3, 2: 0}
      .flatMap((k, v) => ['$k$v', '$k$v']); // ['13', '13', '20', '20']
  print('{1: 3, 2: 0} duplicated => $duplicated');
}

void rangesExample() {
  Range.upTo(3).forEach(print); // => 0, 1, 2, 3
  Range.upTo(4, step: 2).forEach(print); // => 0, 2, 4
  1.upTo(3).forEach(print); // => 1, 2, 3
  1.until(3).forEach(print); // => 1, 2
  3.downTo(1).forEach(print); // => 3, 2, 1
  3.downUntil(1).forEach(print); // => 3, 2
}

void curryingExample() {
  final multiply2 = partial(multiply, 2);
  print(multiply2(3)); // => 6

  final subtract2 = partialRight(subtract, 2);
  final subtractBy2 = partial(subtract, 2);
  print(subtract2(3)); // => (3 - 2) => 1
  print(subtractBy2(3)); // => (2 - 3) => -1
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  collection_ext: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:collection_ext/all.dart';
import 'package:collection_ext/currying.dart';
import 'package:collection_ext/iterables.dart';
import 'package:collection_ext/maps.dart';
import 'package:collection_ext/numbers.dart';
import 'package:collection_ext/ranges.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Feb 12, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
tuple ^1.0.3 1.0.3
Transitive dependencies
matcher 0.12.6
meta 1.1.8
path 1.6.4
quiver 2.1.2+1
stack_trace 1.9.3
Dev dependencies
pedantic ^1.8.0
test ^1.9.4