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

/p/moul/x/daily/multiset/v0

Directory · 3 Files
README.md Open

gno.land/p/moul/x/daily/multiset/v0

Bag / frequency counterNew, FromSlice, Add, AddN, Remove, RemoveN, RemoveAll, Count, MostCommon, Union, Intersect, Sum, Elements, Expand, Clone, MaxDistinct.

1import "gno.land/p/moul/x/daily/multiset/v0"
2
3m := multiset.FromSlice([]string{"a", "a", "a", "b", "b", "c"})
4m.Count("a")        // 3
5m.MostCommon(2)     // [{a 3} {b 2}]
6m.Total()           // 6 occurrences
7m.Distinct()        // 3 elements

The STL multiset and Python's collections.Counter in one type.

MostCommon has a total order: count descending, then element ascending. Sorting by count alone would leave ties in whatever order the backing map yielded — unspecified in gno, and enough to make two nodes render different tables from identical state. The tiebreak is not decoration.

Semantics worth knowing, each with a test:

  • A count reaching zero removes the element, rather than leaving a zero-count ghost that Distinct would still count.
  • RemoveN clamps: removing more than are present clears the element instead of going negative.
  • Union takes the max of each count, Intersect the min (common elements only), Sum adds them.

MaxDistinct (4096) bounds the number of distinct elements; counts themselves are unbounded, so a full set still accepts more occurrences of what it holds.

Live demo: r/moul/x/daily/multisetdemo · render it at /r/moul/x/daily/multisetdemo/v0.


Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.

🧪 Highly experimental — potentially vibe-coded. Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: DISCLAIMER.