Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/multisetdemo/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186411 max_deposit = "5000000ugnot"
multisetdemo.gno
// Package multisetdemo is a small gnoweb demo of the bag / frequency counter
// provided by the [p/moul/x/daily/multiset](/p/nym-moultest123/x/daily/multiset/v0)
// library: word frequencies, ranking, and multiset algebra.
//
// It contains no counting logic of its own. Stateless, so Render is
// deterministic — which is precisely what the library is for.
package multisetdemo
import (
"strconv"
"strings"
"gno.land/p/nym-moultest123/x/daily/multiset/v0"
)
const sample = "the quick brown fox jumps over the lazy dog the fox barks and the dog barks"
// Render renders the demo for gnoweb.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Multiset\n\n")
b.WriteString("A bag that counts duplicates, demoing the ")
b.WriteString("[`p/moul/x/daily/multiset`](/p/nym-moultest123/x/daily/multiset/v0) library.\n\n")
m := multiset.FromSlice(strings.Split(sample, " "))
b.WriteString("## Word frequencies\n\n")
b.WriteString("> " + sample + "\n\n")
b.WriteString("`" + strconv.Itoa(m.Total()) + "` words, `")
b.WriteString(strconv.Itoa(m.Distinct()) + "` distinct.\n\n")
b.WriteString("## `MostCommon(5)`\n\n")
b.WriteString("| rank | word | count |\n|---|---|---|\n")
for i, e := range m.MostCommon(5) {
b.WriteString("| " + strconv.Itoa(i+1) + " | `" + e.Elem + "` | " +
strconv.Itoa(e.Count) + " |\n")
}
b.WriteString("\nNote the ties. `barks`, `dog` and `fox` all occur twice and are ")
b.WriteString("ranked **alphabetically** — the order is count descending, then ")
b.WriteString("element ascending. That second key is not decoration: without it the ")
b.WriteString("ranking would fall back on map iteration order, which gno leaves ")
b.WriteString("unspecified, and two nodes could render different tables.\n\n")
b.WriteString("## Algebra\n\n")
a := multiset.FromSlice([]string{"x", "x", "x", "y"})
c := multiset.FromSlice([]string{"x", "x", "z"})
b.WriteString("With `A = {x:3, y:1}` and `B = {x:2, z:1}`:\n\n")
b.WriteString("| op | meaning | result |\n|---|---|---|\n")
b.WriteString("| `Union` | max of each count | " + brief(a.Union(c)) + " |\n")
b.WriteString("| `Intersect` | min, common only | " + brief(a.Intersect(c)) + " |\n")
b.WriteString("| `Sum` | counts added | " + brief(a.Sum(c)) + " |\n")
b.WriteString("\n## Removal\n\n")
d := multiset.FromSlice([]string{"a", "a", "a"})
b.WriteString("Starting from " + brief(d) + ":\n\n")
d.Remove("a")
b.WriteString("- after `Remove(\"a\")` → " + brief(d) + "\n")
d.RemoveN("a", 100)
b.WriteString("- after `RemoveN(\"a\", 100)` → " + brief(d) +
" — over-removing clears the element instead of going negative\n")
return b.String()
}
func brief(m *multiset.MultiSet) string {
if m.IsEmpty() {
return "`{}`"
}
parts := []string{}
for _, e := range m.Elements() {
parts = append(parts, e+":"+strconv.Itoa(m.Count(e)))
}
return "`{" + strings.Join(parts, ", ") + "}`"
}
render_example_test.gno
package multisetdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Multiset
//
// A bag that counts duplicates, demoing the [`p/moul/x/daily/multiset`](/p/nym-moultest123/x/daily/multiset/v0) library.
//
// ## Word frequencies
//
// > the quick brown fox jumps over the lazy dog the fox barks and the dog barks
//
// `16` words, `10` distinct.
//
// ## `MostCommon(5)`
//
// | rank | word | count |
// |---|---|---|
// | 1 | `the` | 4 |
// | 2 | `barks` | 2 |
// | 3 | `dog` | 2 |
// | 4 | `fox` | 2 |
// | 5 | `and` | 1 |
//
// Note the ties. `barks`, `dog` and `fox` all occur twice and are ranked **alphabetically** — the order is count descending, then element ascending. That second key is not decoration: without it the ranking would fall back on map iteration order, which gno leaves unspecified, and two nodes could render different tables.
//
// ## Algebra
//
// With `A = {x:3, y:1}` and `B = {x:2, z:1}`:
//
// | op | meaning | result |
// |---|---|---|
// | `Union` | max of each count | `{x:3, y:1, z:1}` |
// | `Intersect` | min, common only | `{x:2}` |
// | `Sum` | counts added | `{x:5, y:1, z:1}` |
//
// ## Removal
//
// Starting from `{a:3}`:
//
// - after `Remove("a")` → `{a:2}`
// - after `RemoveN("a", 100)` → `{}` — over-removing clears the element instead of going negative
}