Collections Cheat Sheet

Common Collection Types

Class

Location

Description

<array>

dylan:dylan

Multi-dimensional data, much like a matrix.

<vector>

dylan:dylan

A one-dimensional array. Vectors provide fast random access.

<stretchy-vector>

dylan:dylan

A vector that can have elements directly added and removed without requiring a complete copy.

<list>

dylan:dylan

A linked list.

<deque>

dylan:dylan

A double-ended queue.

<set>

collections:set

A set is for efficiently tracking membership.

<bit-set>

collections:bit-set

A set with bits for members.

<bit-vector>

collections:bit-vector

A vector optimized for storing bit values.

<byte-vector>

collections:byte-vector

A vector of bytes.

<table>

dylan:dylan

A mapping between keys and values.

<string-table>

collections:table-extensions

A <table> with strings for keys.

Note

Because of the default comparison function for <table>, it is important to use <string-table> when the keys will be strings.

Mutable vs. Immutable

Many operations don’t modify the collection passed in. The exception is when you use an operation ending in ! on a stretchy collection.

For this reason, if you want to modify a collection frequently, be sure to pay attention to the type of collection that you’re working with.

In many cases, you will need to assign the result of an operation back to the source collection:

threads := add(threads, thread);

In cases where you are modifying a collection frequently, you may want to consider using a <stretchy-vector> rather than a <vector> or some other type of collection.

Common Operations

Class

Operation

Summary

<collection>

any?

Returns the first true value obtained by iterating over one or more collections.

do

Iterates over one or more collections for side effect.

empty?

Returns true if its argument is empty.

every?

Returns true if a predicate returns true when applied to all corresponding elements of a set of collections.

fill!

Fills a collection with a specified value.

find-key

Returns the key in a collection such that the corresponding collection element satisfies a predicate.

key-sequence

Returns a sequence containing the keys of its collection argument. This is commonly used with <table> instances.

map

Iterates over one or more collections and collects the results in a freshly allocated collection. See also map-as and map-into.

member?

Returns true if a collection contains a particular value.

reduce

Combines the elements of a collection and a seed value into a single value by repeatedly applying a binary function. See also reduce1.

replace-elements!

Replaces those collection elements that satisfy a predicate.

size

Returns the size of its argument.

<sequence>

add / add!

Adds an element to a sequence.

add-new / add-new!

Adds a new element to a sequence.

choose

Returns those elements of a sequence that satisfy a predicate. See also choose-by.

concatenate

Returns the concatenation of one or more sequences in a sequence of a type determined by the type-for-copy of its first argument. See also concatenate-as.

copy-sequence

Returns a freshly allocated copy of some subsequence of a sequence.

intersection

Returns the intersection of two sequences.

remove / remove!

Removes an element from a sequence.

remove-duplicates

Returns a sequence without duplicates.

replace-subsequence!

Replaces a portion of a sequence with the elements of another sequence.

reverse / reverse!

Returns a sequence with elements in the reverse order of its argument sequence.

sort / sort!

Returns a sequence containing the elements of its argument sequence, sorted.

subsequence-position

Returns the position where a pattern appears in a sequence.

union

Returns the union of two sequences.

<list>

head

Returns the head of a list.

tail

Returns the tail of a list.

<deque>

push

Adds an element to the front of a deque.

pop

Removes and returns the first element of a deque.

push-last

Adds an element to the end of a deque.

pop-last

Removes and returns an element from the end of a deque.

<table>

remove-key!

Modifies an explicit key collection so it no longer has a particular key.