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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render_example_test.gno

1.68 Kb · 55 lines
 1package flatmapdemo
 2
 3// ExampleRender pins the realm's Render output as a testable example.
 4func ExampleRender() {
 5	print(Render(""))
 6	// Output:
 7	// # Flat Map
 8	//
 9	// A map backed by sorted slices, demoing the [`p/moul/x/daily/flatmap`](/p/nym-moultest123/x/daily/flatmap/v0) library.
10	//
11	// ## Sorted by construction
12	//
13	// Inserted as `delta, alpha, echo, bravo, charlie` — stored sorted:
14	//
15	// | # | key | value |
16	// |---|---|---|
17	// | 0 | `alpha` | `2` |
18	// | 1 | `bravo` | `4` |
19	// | 2 | `charlie` | `5` |
20	// | 3 | `delta` | `1` |
21	// | 4 | `echo` | `3` |
22	//
23	// No sort on read, and no dependence on map iteration order — which gno leaves unspecified, and which would let two nodes render different pages from the same state.
24	//
25	// ## Indexed access
26	//
27	// Sorted storage gives positional lookup that a hash map cannot:
28	//
29	// - `At(0)` → `alpha` = `2`
30	// - `At(2)` → `charlie` = `5`
31	// - `At(4)` → `echo` = `3`
32	//
33	// ## Range queries
34	//
35	// Two binary searches and a walk — `lo` inclusive, `hi` exclusive:
36	//
37	// | range | keys |
38	// |---|---|
39	// | `["bravo", "delta")` | `bravo`, `charlie` |
40	// | `["charlie", ∞)` | `charlie`, `delta`, `echo` |
41	// | `["b", "d")` | `bravo`, `charlie` |
42	//
43	// The last one asks for bounds that are not keys at all — the search still lands in the right place.
44	//
45	// ## The trade
46	//
47	// | operation | cost |
48	// |---|---|
49	// | `Get` | O(log n) binary search over contiguous memory |
50	// | iteration | O(n), already ordered, nothing to sort |
51	// | `Set` in the middle | **O(n)** — the tail shifts |
52	// | `Set` at the end | O(1) amortised — the fast path |
53	//
54	// Cheap reads and cheap ordered iteration, paid for at write time.
55}