on master help sign in
updated dojo responses, clarification, link, typo
c997318poprox
learn/hoon/hoon-tutorial/trees-sets-and-maps.md+31−30
@@ -102,26 +102,27 @@ Using `sy`:
102[n=11 l={} r={22 33 44 55}]
102[n=11 l={} r={22 33 44 55}]
103
103
104> `(set @)`c
104> `(set @)`c
105−{11 22 33 44 55}
105+{11 22 55 33 44}
106
106
107> =c `(set @)`c
107> =c `(set @)`c
108```
108```
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.
109
110
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`:
111
112
112```
113```
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}]
115
116
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}
118```
119```
119
120
120You can remove items using `del` of `in`:
121You can remove items using `del` of `in`:
121
122
122```
123```
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}]
125
126
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
140
141
141```
142```
142> c
143> c
143−{11 22 33 44 55}
144+{11 22 55 33 44}
144
145
145> (~(run in c) |=(a=@ +(a)))
146> (~(run in c) |=(a=@ +(a)))
146−{12 23 34 45 56}
147+{56 45 23 12 34}
147```
148```
148−
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`:
150
150
151```
151```
152> c
152> c
153−{11 22 33 44 55}
153+{11 22 55 33 44}
154
154
155> (~(rep in c) add)
155> (~(rep in c) add)
156b=165
156b=165
@@ -160,24 +160,24 @@ The standard library also has the union and intersection functions for sets:
160
160
161```
161```
162> c
162> c
163−{11 22 33 44 55}
163+{11 22 55 33 44}
164
164
165> =d `(set @)`(sy ~[33 44 55 66 77])
165> =d `(set @)`(sy ~[33 44 55 66 77])
166
166
167> d
167> d
168−{33 44 55 66 77}
168+{66 77 55 33 44}
169
169
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={}]]
172
172
173> `(set @)`(~(int in c) d)
173> `(set @)`(~(int in c) d)
174−{33 44 55}
174+{55 33 44}
175
175
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}]]
178
178
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}
181```
181```
182
182
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}
188
188
189> ~(tap in c)
189> ~(tap in c)
190−~[55 44 33 22 11]
190+~[44 33 55 22 11]
191```
191```
192
192
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.
194
194
195### Cartesian Product
195### Cartesian Product
196
196
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`.
198
198
199```
199```
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])
220
220
221> c
221> c
222−{1 3 2}
222+{1 2 3}
223
223
224> =d `(set @)`(sy ~[4 5 6])
224> =d `(set @)`(sy ~[4 5 6])
225
225
226> d
226> d
227−{5 4 6}
227+{5 6 4}
228
228
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]}
231```
231```
232
232
233## Maps
233## Maps
234
234
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.
236
236
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])`.
238
238
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.
240
240
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.
242
242
@@ -246,36 +246,36 @@ Using `my`:
246> =c (my ~[[%one 1] [%two 2] [%three 3]])
246> =c (my ~[[%one 1] [%two 2] [%three 3]])
247
247
248> c
248> c
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]}]
250
250
251> =c `(map @tas @)`c
251> =c `(map @tas @)`c
252
252
253> c
253> 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]}
255```
255```
256
256
257We can add a key-value pair with `put` of `by`:
257We can add a key-value pair with `put` of `by`:
258
258
259```
259```
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]}]
262
262
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]}
265```
265```
266
266
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:
268
268
269```
269```
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]}
272```
272```
273
273
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:
275
275
276```
276```
277> (~(get by c) %two)
277> (~(get by c) %two)
278−[~ u=2]
278+[~ 2]
279```
279```
280
280
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
287
287
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.
289
289
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.
291
291
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:
293
293
@@ -297,7 +297,7 @@ If you want to get some key value without producing a unit, use `got` instead:
297
297
298> (~(got by c) %chicken)
298> (~(got by c) %chicken)
299####
299####
300−ford: build failed
300+ford: %ride failed to execute:
301```
301```
302
302
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
314
314
315```
315```
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]}
318```
318```
319
319
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.
321
321
322### [Next Up: <placeholder>]
322### [Next Up: <placeholder>]
323+