on 77a6721 help sign in
urbit.org/docs/hoon/exercises/data-structures.md
62 lines 1.9 kB history raw
1---
2navhome: /docs
3sort: 4
4comments: true
5---
7# Sets and Maps
9Sets are constructed with `++silt`. A set is a non-ordered
10collection of unique elements.
12```
13(silt `(list @t)`['urbit' 'arvo' 'hoon' 'arvo' ~])
14```
16We'll refer to this particular set as `s` in the exercises. It
17is of type `(set @t)` since each element is a `@t`.
19The `++in` core contains library functions for dealing with sets.
20For example, `++has:in` checks whether an element is in the set.
21Thus, `(~(has in s) 'arvo')` returns yes. To complete these
22exercises, you'll need to refer to the [library reference for
23`++in`](/docs/hoon/library/2h).
251. Create another set, `l`, with members `'c'`, `'hoon'`,
26 `'javacript'`, `'python'`.
281. At the dojo, take the union of `s` and `l`. Delete
29 `'javascript'` from the result, and add `'coffescript'`.
311. Create an alphabetized list of the members of the set created
32 in the previous exercise.
341. Take the Cartesian product of `s` and `l`. The Cartesian
35 product of two sets is the set of all cells `[a b]` where
36 `a` is in `s` and `b` is in `l`.
38The `++by` core is to maps as `++in` is to sets. Refer to its
39[library reference](/docs/hoon/library/2i). Note that since a
40map is basically a set of pairs (although not every set of pairs
41is a map), most functions in `++in` have analogues in `++by`.
43The constructor for maps is `++malt`.
45```
46(malt `(list {@t @ud})`[['kaylee' 3] ['wash' 2] ['mal' 4] ['summer' 1] ~])
47```
49Let this map be `m`.
511. Create another map, `s`, with `'spring'` mapped to `1`,
52 `'summer'` mapped to `2`, `'autumn'` mapped to `'3'`, and
53 `'winter'` mapped to `4`.
551. In `m`, change the value at `'mal'` to 5 and add `'cobb'`
56 with value 4.
581. Take the union of `m` and `s`, then get the value at the key
59 `summer`. Try taking the union of `s` and `m` (i.e. flip the
60 order) and see if it makes a difference. Try intersecting
61 them in both orders.