Collections
Ribs provides it's own collection framework, heavily inspired by the Scala standard collections library. It's primary aims are:
- Provide immutable variants of standard collection types
- Provide an expressive and powerful API
- Provide comparable performance to the standart Dart library collections
This page will only give a very short and incomplete example of each collection type. It's highly recommended to explore the API for each collection type to get a better sense of what capabilities each provides.
IList
An linked-list, ordered collection of 0 or more elements of the same type.
final l = IList.range(0, 10);
final plusOne = l.map((n) => n + 1);
final odds = l.filter((n) => n.isOdd);
final maybeFirstElement = l.headOption;
final numLessThan5 = l.count((n) => n < 5);
final combined = l.concat(l);
final dropLast3 = l.dropRight(3);
final anyBigNumbers = l.exists((a) => a > 100);
final everyoneLessThan1000 = l.forall((a) => a < 1000);
final maybe4 = l.find((a) => a == 4);
IVector
An ordered collection of 0 or more elements of the same type. IVector uses
finger trees to achieve generally good, balanced performance across all
operations. This can make it a better option than IList
for many operations
that linked lists don't do well with.
final v = ivec([1, 2, 3]);
final perms = v.permutations();
final sliding = v.sliding(2, 2);
final evensMinus1 = v.collect((x) => Option.when(() => x.isEven, () => x - 1));
final scanVec = v.scan(0, (a, b) => a + b);
final sortedVec = v.sorted(Order.ints.reverse());
IChain
An ordered collection of 0 or more elements of the same type. IChain provides constant
time append
, prepend
and concat
operations which make it ideal for certain
circumstances.
NonEmptyIList
An ordered collection of 1 or more elements of the same type.
final nonEmptyList = nel(1, [2, 3, 4, 5]);
final first = nonEmptyList.head;
final nelOdds = nonEmptyList.filter((a) => a.isOdd);
Range
Represents values in a range (inclusive or exclusive) between a start and end value, with a positive step
value used to calculate successive elements.
final rInc = Range.inclusive(0, 5).tapEach(print); // 0, 1, 2, 3, 4, 5
final rExc0 = Range.exclusive(0, 5).by(2).tapEach(print); // 0, 2, 4
final rExc1 = Range.exclusive(0, 5, 2).tapEach(print); // 0, 2, 4
IMap
A collection of Key-Value pairs. Values are typically accessed by providing the associated key.
final map = imap({
'red': 1,
'orange': 2,
'yellow': 3,
'green': 4,
'blue': 5,
'indigo': 6,
'violet': 7,
});
final updatedMap = map.updated('green', 90);
final defaultValue = map.withDefaultValue(-1);
final defaultValue2 = map.withDefault((key) => key.length);
ISet
A collection of unique element of the same type. There are no duplicate
elements, as defined by each elements ==
method.
final aSet = iset([1, 3, 5, 7, 9]);
final remove5 = aSet.excl(5);
final remove1and9 = aSet.removedAll(iset([1, 9]));
final add11 = aSet + 11;
ListBuffer
A mutable ordered collection, analogous to IList.
MMap
A mutable collection analogous to IMap.
MSet
A mutable collection analogous to ISet.