Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

render_example_test.gno

1.65 Kb · 54 lines
 1package bidimapdemo
 2
 3// ExampleRender pins the realm's Render output as a testable example.
 4func ExampleRender() {
 5	print(Render(""))
 6	// Output:
 7	// # Bidirectional Map
 8	//
 9	// Unique in both directions, demoing the [`p/moul/x/daily/bidimap`](/p/moul/x/daily/bidimap/v0) library.
10	//
11	// ## Roles
12	//
13	// | person | role |
14	// |---|---|
15	// | `alice` | `admin` |
16	// | `bob` | `auditor` |
17	// | `carol` | `treasurer` |
18	//
19	// ## Both directions are O(1)
20	//
21	// - `Get("bob")` → `auditor`
22	// - `GetKey("treasurer")` → `carol` — the reverse index, not a scan
23	//
24	// ## Displacing a binding
25	//
26	// Both sides are unique, so giving `alice` the `auditor` role cannot simply be written — `bob` already holds it, and `alice` already holds `admin`. `Put` **replaces**, and reports what it displaced:
27	//
28	// | displaced key | displaced value |
29	// |---|---|
30	// | `alice` | `admin` |
31	// | `bob` | `auditor` |
32	//
33	// | person | role |
34	// |---|---|
35	// | `alice` | `auditor` |
36	// | `carol` | `treasurer` |
37	//
38	// `bob` is now unassigned and the `admin` role is vacant — one call, both indexes still agreeing.
39	//
40	// ## Refusing instead
41	//
42	// `PutUnique` makes the other choice: it declines rather than displace.
43	//
44	// - `PutUnique("dave", "auditor")` → `false` — the role is taken
45	// - `PutUnique("dave", "secretary")` → `true` — both sides free
46	//
47	// | person | role |
48	// |---|---|
49	// | `alice` | `auditor` |
50	// | `carol` | `treasurer` |
51	// | `dave` | `secretary` |
52	//
53	// > Keys come back **sorted**, never in map order: gno map iteration order is unspecified, and a page built from one can differ between nodes — a consensus bug, not a cosmetic one.
54}