Unison provides a variety of data types for managing collections of values. We'll show the basics of how to create instances of common collection types here.
Lists
One of the common things you'll be doing as a Unison programmer is managing ordered collections of one type or another. One of Unison's native data structures for this is List
, which we can create between square brackets.
desserts : [Text]
desserts = ["Eclair", "Peach cobbler", "Ice cream"]
Lists can only contain values of one type at a time, and are eagerly evaluated.
An empty list is simply represented []
or with List.empty
.
For the curious, the documentation for List
describes the underlying data structure and details many of the common operations you might perform on List
s.
Maps
The data.Map
type is Unison's way of mapping unique keys to values.
You can create a map with a single object with Map.singleton 1 "a"
where 1
is the key and "a"
is the value associated with that key.
Currently Unison does not have special data.Map
construction syntax so one easy way to create a multi item map is from a List
of tuples
distributedDatasets.lib.base.data.Map.fromList
[(1, "a"), (2, "b"), (3, "c")]⧨internal.Bin
3
2
"b"
(internal.Bin 1 1 "a" internal.Tip internal.Tip)
(internal.Bin 1 3 "c" internal.Tip internal.Tip)
What's printed above for a data.Map
might look more complicated than it really is. internal.Bin
and internal.Tip
are just the data constructors for data.Map
. You'll likely be working with Maps through functions in the base library instead of dealing with these terms directly. Check out a few data.Map
manipulation functions with find base.Map
in the UCM.
Issue the docs Map
command in the UCM to read the data.Map
documentation itself.
Sets
You can create a Set
from a list of elements with the distributed.lib.base.data.Set.fromList
constructor
distributed.lib.base.data.Set.fromList [1, 2, 3, 3, 4]⧨internal.Set
(internal.Bin
4
2
()
(internal.Bin 1 1 () internal.Tip internal.Tip)
(internal.Bin
2
3
()
internal.Tip
(internal.Bin 1 4 () internal.Tip internal.Tip)))
Creating a Set
value enables the efficient functions like Set.contains
and Set.union
that you might be familiar with from other languages.
You can explore these types and more of other collection apis in the base
library.