/immutable-dart

Immutable, persistent data structures for Dart and Flutter.

Primary LanguageDartApache License 2.0Apache-2.0

Immutable.dart

A mininimal set of collections which are

  • Immutable: collections cannot be mutated, instead returning new collections when items are added or removed.
  • Persistent: collections share unchanged data across versions.

Features

  • == can be used to check if a Collection has changed. This allows efficient update methods in AngularDart 2 , Flutter, and OverReact.
  • Producing a new collection is approximately O(logN), instead of O(N) for a full copy.
  • Field access is Log32N, which is only marginally slower than constant time for reasonably sized (less than 100 trillion elements) collections.

Examples

Vector

Creating a Vector from an Iterable.

	final vec = new Vector.fromIterable(new List.generate(64000, (i) => i));
	print(vec[2000]); // prints "2000"

Transforming a Vector using Iterable methods.

	final sum = vec
    	.map((i) => 2 * i)
        .filter((i) => i % 2 == 0)
        .fold(0, (x, y) => x + y));

Using a Vector as an immutable Stack

	var stack = new Vector.empty();
    stack = stack.append(2);
    stack = stack.append(3);
    stack = stack.append(4);
    while (stack.isNotEmpty) {
    	print(stack.last); // prints "4", then "3", then "2"
        stack = stack.removeLast();
    }
    print(stack.length); // prints "0"

Dictionary

Creating a Dictionary from a Map.

    final dict = new Dictionary<String, int>.from({'one': 1, 'two': 2, 'three': 3});
    print(dict['one']); //prints "1"

Adding and removing values from the Dictionary.

    var cache = new Dictionary<String, int>.empty();
    cache = cache.assoc("foo", 2);
    cache = cache.assoc("bar", 3);
    cache = cache.remove("foo");
    print(cache["foo"]); // prints "null"
    print(cache["bar"]); // print "3"

Todo

  • Support transients for efficient mutations (internally and externally)
  • Finish Vector, Dictionary, and LazyIterable APIs (sort, flatMap?)
  • Large benchmark tests