@@ -102,26 +102,27 @@ Using `sy`:
102[n=11 l={} r={22 33 44 55}]
102[n=11 l={} r={22 33 44 55}]
104> `(set @)`c
104> `(set @)`c
105−{11 22 33 44 55}
105+{11 22 55 33 44}
107> =c `(set @)`c
107> =c `(set @)`c
109+Note that Dojo does not necessarily print elements of a set in the same order they were given. Mathematically speaking, sets are not ordered, so this is alright. There is no difference between two sets with the same elements written in a different order. Try forming `c` with a different ordering and check this.
110And we can add an item to the set using `put` of `in`:
111And we can add an item to the set using `put` of `in`:
113> (~(put in c) 77)
114> (~(put in c) 77)
114−[n=11 l=~ r=[n=77 l={22 33 44 55} r={}]]
115+[n=11 l={} r={22 77 55 33 44}]
116> `(set @)`(~(put in c) 77)
117> `(set @)`(~(put in c) 77)
117−{11 22 33 44 55 77}
118+{11 22 77 55 33 44}
120You can remove items using `del` of `in`:
121You can remove items using `del` of `in`:
123> (~(del in c) 55)
124> (~(del in c) 55)
124−[n=11 l=~ r=[n=33 l={22} r={44}]]
125+[n=11 l={} r={22 33 44}]
126> `(set @)`(~(del in c) 55)
127> `(set @)`(~(del in c) 55)
127{11 22 33 44}
128{11 22 33 44}
@@ -140,17 +141,16 @@ You can apply a gate to each item of a set using `run` of `in` and produce a new
143−{11 22 33 44 55}
144+{11 22 55 33 44}
145> (~(run in c) |=(a=@ +(a)))
146> (~(run in c) |=(a=@ +(a)))
146−{12 23 34 45 56}
147+{56 45 23 12 34}
149You can also apply a gate to all items of the set and return an accumulated value using `rep` of `in`:
149You can also apply a gate to all items of the set and return an accumulated value using `rep` of `in`:
153−{11 22 33 44 55}
153+{11 22 55 33 44}
155> (~(rep in c) add)
155> (~(rep in c) add)
@@ -160,24 +160,24 @@ The standard library also has the union and intersection functions for sets:
163−{11 22 33 44 55}
163+{11 22 55 33 44}
165> =d `(set @)`(sy ~[33 44 55 66 77])
165> =d `(set @)`(sy ~[33 44 55 66 77])
168−{33 44 55 66 77}
168+{66 77 55 33 44}
170> (~(int in c) d)
170> (~(int in c) d)
171−[n=33 l={} r={44 55}]
171+[n=33 l=[n=55 l={} r={}] r=[n=44 l={} r={}]]
173> `(set @)`(~(int in c) d)
173> `(set @)`(~(int in c) d)
174−{33 44 55}
174+{55 33 44}
176> (~(uni in c) d)
176> (~(uni in c) d)
177−[n=11 l={} r={22 33 44 55 66 77}]
177+[n=11 l=[n=66 l={} r={}] r=[n=33 l={22 77 55} r={44}]]
179> `(set @)`(~(uni in c) d)
179> `(set @)`(~(uni in c) d)
180−{11 22 33 44 55 66 77}
180+{66 11 22 77 55 33 44}
183It may be convenient to turn a set into a list for some operation and then operate on the list. You can convert a set to a list using `tap` of `in`:
183It may be convenient to turn a set into a list for some operation and then operate on the list. You can convert a set to a list using `tap` of `in`:
@@ -187,14 +187,14 @@ It may be convenient to turn a set into a list for some operation and then opera
187{11 22 33 44 55}
187{11 22 33 44 55}
189> ~(tap in c)
189> ~(tap in c)
190−~[55 44 33 22 11]
190+~[44 33 55 22 11]
193There are other set functions in the Hoon standard library we won't cover here.
193There are other set functions in the Hoon standard library we won't cover here.
195### Cartesian Product
195### Cartesian Product
197−Here's a program that takes two sets of atoms and returns the Cartesian product of those sets. A Cartesian product of two sets `a` and `b` is a set of all the cells whose head is a member of `a` and whose tail is a member of `b`.
197+Here's a program that takes two sets of atoms and returns the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product) of those sets. A Cartesian product of two sets `a` and `b` is a set of all the cells whose head is a member of `a` and whose tail is a member of `b`.
200|= [a=(set @) b=(set @)]
200|= [a=(set @) b=(set @)]
@@ -219,24 +219,24 @@ Save this as `cartesian.hoon` in your urbit's pier and run in the dojo:
219> =c `(set @)`(sy ~[1 2 3])
219> =c `(set @)`(sy ~[1 2 3])
224> =d `(set @)`(sy ~[4 5 6])
224> =d `(set @)`(sy ~[4 5 6])
229> +cartesian [c d]
229> +cartesian [c d]
230−{[1 5] [1 4] [1 6] [3 5] [3 4] [3 6] [2 5] [2 4] [2 6]}
230+{[2 6] [1 6] [3 6] [1 4] [1 5] [2 4] [3 5] [3 4] [2 5]}
235−Use `map` to create a set of key-value pairs, e.g., `(map @tas *)` for a set of `@tas` and `*` pairs. The `@tas` value serves as the 'key', which is a mechanism for tagging or naming the value, `*`. They key of each key-value pair is unique; every value in a map gets its own unique name.
235+Use `map` to create a set of key-value pairs, e.g., `(map @tas *)` for a set of `@tas` and `*` pairs. The `@tas` value serves as the 'key', which is a mechanism for tagging or naming the value, `*`. The key of each key-value pair is unique; every value in a map gets its own unique name.
237One example use case is for storing customer information as a set of pairs: `(map [employee-name employee-data])`.
237One example use case is for storing customer information as a set of pairs: `(map [employee-name employee-data])`.
239−The `by` core in the Hoon standard library contains the various functions for operating on maps. Many of these functions are similar to the set functions of the `in` core. See the standard library reference documentation for maps [here](/docs/reference/library/2i/). As was the case with sets, the underlying noun of each map is a tree.
239+The `by` core in the Hoon standard library contains the various functions for operating on maps. Many of these functions are similar to the set functions of the `in` core. See the standard library reference documentation for maps here [here](/docs/reference/library/2i/). As was the case with sets, the underlying noun of each map is a tree.
241Two common methods for populating a map include (1) creating it from a list of key-value cells using the `my` function, and (2) inserting items into a map using the `put` arm of the `by` core.
241Two common methods for populating a map include (1) creating it from a list of key-value cells using the `my` function, and (2) inserting items into a map using the `put` arm of the `by` core.
@@ -246,36 +246,36 @@ Using `my`:
246> =c (my ~[[%one 1] [%two 2] [%three 3]])
246> =c (my ~[[%one 1] [%two 2] [%three 3]])
249−[n=[p=%two q=2] l={[p=%three q=3] [p=%one q=1]} r={}]
249+[n=[p=%one q=1] l={[p=%two q=2]} r={[p=%three q=3]}]
251> =c `(map @tas @)`c
251> =c `(map @tas @)`c
254−{[p=%three q=3] [p=%one q=1] [p=%two q=2]}
254+{[p=%two q=2] [p=%one q=1] [p=%three q=3]}
257We can add a key-value pair with `put` of `by`:
257We can add a key-value pair with `put` of `by`:
260> (~(put by c) [%four 4])
260> (~(put by c) [%four 4])
261−[n=[p=%two q=2] l={[p=%three q=3] [p=%four q=4] [p=%one q=1]} r={}]
261+[n=[p=%one q=1] l={[p=%four q=4] [p=%two q=2]} r={[p=%three q=3]}]
263> `(map @tas @)`(~(put by c) [%four 4])
263> `(map @tas @)`(~(put by c) [%four 4])
264−{[p=%three q=3] [p=%four q=4] [p=%one q=1] [p=%two q=2]}
264+{[p=%four q=4] [p=%two q=2] [p=%one q=1] [p=%three q=3]}
267Delete a key-value pair with `del` of `by`, keeping in mind that you only need to pick the key of the pair to be deleted:
267Delete a key-value pair with `del` of `by`, keeping in mind that you only need to pick the key of the pair to be deleted:
270> `(map @tas @)`(~(del by c) %two)
270> `(map @tas @)`(~(del by c) %two)
271−{[p=%three q=3] [p=%one q=1]}
271+{[p=%one q=1] [p=%three q=3]}
274You can produce the value of a key-value pair using `get` of `by` on the key:
274You can produce the value of a key-value pair using `get` of `by` on the key:
277> (~(get by c) %two)
277> (~(get by c) %two)
281This result may be unexpected. Why didn't it just give us `2`? The answer has to do with the possibility that a requested key may not be in the map. For example:
281This result may be unexpected. Why didn't it just give us `2`? The answer has to do with the possibility that a requested key may not be in the map. For example:
@@ -287,7 +287,7 @@ This result may be unexpected. Why didn't it just give us `2`? The answer has
288Because there is no `%chicken` key in `c`, `get` simply returns `~` to indicate it's not in the map. Otherwise it returns a pair like the one you see in the next to last example.
288Because there is no `%chicken` key in `c`, `get` simply returns `~` to indicate it's not in the map. Otherwise it returns a pair like the one you see in the next to last example.
290−`get` of `by` returns the key's value as a `unit`, not as raw data. There are two kinds of `unit`s: null `~`, and non-null. A non-null `unit` is a pair of `[~ u=value]`. Unit types are constructed the way list, set, and map types are; for example, `(unit @)` is the type for a unit whose value is an atom.
290+`get` of `by` returns the key's value as a `unit`, not as raw data. There are two kinds of `unit`s: null `~`, and non-null. A non-null `unit` is a pair of `[~ value]`. Unit types are constructed the way list, set, and map types are; for example, `(unit @)` is the type for a unit whose value is an atom.
292If you want to get some key value without producing a unit, use `got` instead:
292If you want to get some key value without producing a unit, use `got` instead:
@@ -297,7 +297,7 @@ If you want to get some key value without producing a unit, use `got` instead:
298> (~(got by c) %chicken)
298> (~(got by c) %chicken)
300−ford: build failed
300+ford: %ride failed to execute:
303Use `has` of `by` to see whether a certain key is in the map:
303Use `has` of `by` to see whether a certain key is in the map:
@@ -314,9 +314,10 @@ You can use `run` of `by` to apply a gate to each value in a map, producing a ma
316> (~(run by c) |=(a=@ (mul 2 a)))
316> (~(run by c) |=(a=@ (mul 2 a)))
317−{[p=%three q=6] [p=%one q=2] [p=%two q=4]}
317+{[p=%two q=4] [p=%one q=2] [p=%three q=6]}
320There are other map functions in the Hoon standard library that didn't cover here.
320There are other map functions in the Hoon standard library that didn't cover here.
322### [Next Up: <placeholder>]
322### [Next Up: <placeholder>]