on 2d236ab help sign in
urbit.org/docs/hoon/exercises/data-structures.md
61 lines 1.9 kB history raw
1---
2sort: 4
3comments: true
4---
6# Sets and Maps
8Sets are constructed with `++silt`. A set is a non-ordered
9collection of unique elements.
11```
12(silt `(list @t)`['urbit' 'arvo' 'hoon' 'arvo' ~])
13```
15We'll refer to this particular set as `s` in the exercises. It
16is of type `(set @t)` since each element is a `@t`.
18The `++in` core contains library functions for dealing with sets.
19For example, `++has:in` checks whether an element is in the set.
20Thus, `(~(has in s) 'arvo')` returns yes. To complete these
21exercises, you'll need to refer to the [library reference for
22`++in`](/docs/hoon/library/2h).
241. Create another set, `l`, with members `'c'`, `'hoon'`,
25 `'javacript'`, `'python'`.
271. At the dojo, take the union of `s` and `l`. Delete
28 `'javascript'` from the result, and add `'coffescript'`.
301. Create an alphabetized list of the members of the set created
31 in the previous exercise.
331. Take the Cartesian product of `s` and `l`. The Cartesian
34 product of two sets is the set of all cells `[a b]` where
35 `a` is in `s` and `b` is in `l`.
37The `++by` core is to maps as `++in` is to sets. Refer to its
38[library reference](/docs/hoon/library/2i). Note that since a
39map is basically a set of pairs (although not every set of pairs
40is a map), most functions in `++in` have analogues in `++by`.
42The constructor for maps is `++malt`.
44```
45(malt `(list {@t @ud})`[['kaylee' 3] ['wash' 2] ['mal' 4] ['summer' 1] ~])
46```
48Let this map be `m`.
501. Create another map, `s`, with `'spring'` mapped to `1`,
51 `'summer'` mapped to `2`, `'autumn'` mapped to `'3'`, and
52 `'winter'` mapped to `4`.
541. In `m`, change the value at `'mal'` to 5 and add `'cobb'`
55 with value 4.
571. Take the union of `m` and `s`, then get the value at the key
58 `summer`. Try taking the union of `s` and `m` (i.e. flip the
59 order) and see if it makes a difference. Try intersecting
60 them in both orders.