aboutsummaryrefslogtreecommitdiff
path: root/documentation/book/the_lux_programming_language/chapter_15.md
blob: d30feb1cf7022cead351e26a49981fcf9e198ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Chapter 15: Persistent data structures

_Where you will learn a new way to organize your data._

---

So far, you know how to use variants, tuples and records to structure your data.

You're also familiar with lists, and you might even have come up with clever ways to use lists to implement key-value structures, like _property lists_.

But the reality is that there are many different ways to organize data, and how you implement the mechanisms to do so will have an impact on the performance of your programs.

That is why there are so many different types of data-structures, and so many different implementations for them.

But not all such implementations fit into the functional paradigm of keeping all your data immutable, and most implementations of data-structures are actually mutable, and meant for imperative programming.

Now, let's not be naïve.

Everybody can figure out that making a data-structure immutable and just copying the whole thing every time you want to make a change would make those data-structures prohibitively expensive to use.

Luckily for us, there is a way to have immutable data-structures that have _reasonable performance_.

The reason why they are _fast enough_ to be used, is that they are designed to re-use as many nodes as they can whenever an update needs to be done, in order to avoid wasteful re-work wherever possible.

Make no mistake, they are still not as fast as their mutable counterparts (which you can still access by doing host-interop), but they are designed with high-performance in mind, and so they tend to be _fast enough_ for most use-cases.

Lux offers a variety of these persistent data-structures.

Here are some examples:

## Sequences

	Located in `library/lux/data/collection/sequence`.

These are similar to lists in that they are sequential data-structures, but there are a few differences:

1. Whereas lists prepend values to the front, sequences append them to the back.
2. Random access on lists has a complexity of O(N), whereas it's O(log N) for sequences.

Sequences are a great substitute for lists whenever random access is a must, and their implementation ensures updates are as cheap as possible.

## Queues

	Located in `library/lux/data/collection/queue`.

Queues are the classic first-in first-out (FIFO) data-structure.

Use them whenever processing order matters, but you need to add to the back (unlike lists, where order matters but add to the front).

## Dictionaries

	Located in `library/lux/data/collection/dictionary`.

This is your standard key-value data-structure.

Known by other names (tables, maps, etc), dictionaries give you efficient access and updating functionality.

All you need to do is give it a `Hash` implementation (from `library/lux/abstract/hash`) for your _"key"_ type, and you're good to go.

## Sets

	Located in `library/lux/data/collection/set`.

This is similar to dictionaries in that a `Hash` implementation is needed, but instead of it being a key-value data-structure, it only stores values (and then tells you if any given value is a member of the set).

This is a useful data-structure for modelling group membership and keeping track of things. Plus, there are several set-theoretic operations defined in that module.

## Persistent data structures and Software Transactional Memory

This is a killer combination.

Instead of using mutable data-structures for your changing program data, you can just use persistent data-structures, with the mutability being delegated the the STM system.

This will make working concurrently with these data-structures a piece of cake, since you never have to worry about synchronizing/locking anything to avoid simultaneous updating, or any of the other crazy things programmers have to do to avoid data corruption.

## Arrays: the not-so-persistent data structures

	Located in `library/lux/data/collection/array`.

The `library/lux/data/collection/array` module features mutable arrays you can use if you need fast random access and mutation and are willing to run the risks involved with using mutable data.

Another possible use is to implement other data-structures (and, as it turns out, sequences and dictionaries both rely on arrays for their implementations).

Also, it's worth nothing that in the _JVM_, this corresponds to _object arrays_.

If you want primitive arrays, you should check out the functionality for that in `library/lux/ffi`.

---

It may seem odd that this chapter doesn't feature any code samples, but most of what you need to know is already located in the standard library documentation.

These data-structures are very easy to use and offer decent performance, so you're encouraged to use them to model all your data processing code.

The next chapter is going to be slightly different, in that we're going to be learning not how to write programs, but how to test them.

See you in [the next chapter](chapter_16.md)!