4.1 Choosing Collections
The modules in rebellion/collection
provide a wide selection of
different collections. Choosing the right one can be tricky, so this document
aims to make that decision easier. Treat this as guidance rather than
rules —
4.1.1 To Key or Not to Key
The first question you should ask yourself is, do the items of this collection have keys? Can items be looked up by key? If so, you have a dictionary-like collection, also called a bicollection. There are many different kinds of bicollections that vary in whether or not keys are unique, whether entries are unordered, ordered, or sorted, and whether any type of value can serve as a key or only certain types of values. Bicollections can usually be viewed as a collection of key-value entries.
Bicollections | Not bicollections |
4.1.2 Collection Cheat Sheet
Instead of using... | Consider using... |
An unordered list with no duplicates | A set |
An unordered list | A multiset |
A hash whose values are always #true | A set |
A hash whose values are positive integers representing frequencies | A multiset |
An unordered list of key-value pairs with no duplicate keys | A hash |
An unordered list of key-value pairs | |
A list of key-value pairs where order is significant | |
A keyset | |
A hash whose keys are symbols or strings | A record |
A table |