Common collection types

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 isList,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 areeagerly evaluated.

An empty list is simply represented[]or withList.empty.

For the curious, the documentation forListdescribes the underlying data structure and details many of the common operations you might perform onLists.

Maps

TheMaptype is Unison's way of mapping unique keys to values.

You can create a map with a single object withMap.singleton 1 "a"where1is the key and"a"is the value associated with that key.

Currently Unison does not have specialMapconstruction syntax so one easy way to create a multi item map is from aListof tuples

Map.fromList [(1, "a"), (2, "b"), (3, "c")]
Bin 3 2 "b" (Bin 1 1 "a" Tip Tip) (Bin 1 3 "c" Tip Tip)

What's printed above for aMapmight look more complicated than it really is.BinandTipare just thedata constructorsforMap.You'll likely be working with Maps through functions in the base library instead of dealing with these terms directly. Check out a fewMapmanipulation functions withfind base.Mapin the UCM.

Issue thedocs Mapcommand in the UCM to read theMapdocumentation itself.

Sets

AMapwhose values are all Unit,(),can be translated into aSetby wrapping the map inSet'sdata constructor.

map = Map.fromList [(1, ()), (2, ()), (3, ())] Set.Set map
Set.Set (Bin 3 2 () (Bin 1 1 () Tip Tip) (Bin 1 3 () Tip Tip))

Creating aSetvalue enables the efficient functions likeSet.containsandSet.unionthat you might be familiar with from other languages.

You can explore these types and more of other collection apisin thebaselibrary.