/p/moul/x/daily/multiset/v0
gno.land/p/moul/x/daily/multiset/v0
Bag / frequency counter — New, 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
Distinctwould still count. RemoveNclamps: removing more than are present clears the element instead of going negative.Uniontakes the max of each count,Intersectthe min (common elements only),Sumadds 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.