Description
Summary
Incorporate a new Coins
method: func (c Coins) Min(other Coins) Coins
for utility and consistency.
Problem Definition
When working with Coins
, it's sometimes useful to compute the minimum of two values, defined as the minimum of each denom, but there's no existing library method for it.
Example: toTransfer := want.Min(spendable)
Example: to compute max(a.Sub(b), zeroCoin)
without panicking, compute a.Sub(a.Min(b))
With IsAllLTE()
defining a partial order on Coins
, Min()
becomes the "meet" operator of a lattice, tying the operations into centuries of mathematical precedent.
Proposal
Implement func (c Coins) Min(other Coins) Coins
with tests and documentation for its properties.
Max()
should be added for symmetry, with the property that a.Add(b) == (a.Max(b)).Add(a.Min(b))
, so it could be defined as
func (c Coins) Max(other Coins) Coins {
return c.Add(other).Sub(c.Min(other))
}
Where 0
is the empty Coins
value, <=
is IsAllLTE()
, and ==
is a non-panicking variant of IsEqual()
, for arbitrary Coins
values a
, b
, and c
, we have the properties:
<=
is a partial order:- Reflexivity:
a <= a
- Antisymmetry:
a <= b && b <= a
implya == b
- Transitivity:
a <= b && b <= c
implya <= c
- Reflexivity:
Min()
is the greatest lower bound ("meet") with respect to this order, andMax()
is the least upper bound ("join"):a <= b
if and only ifa.min(b) == a && a.max(b) == b
Min()
andMax()
are commutative and associative- Absorption laws:
a.Max(a.Min(b)) == a
a.Min(a.Max(b)) == a
- Distributivity:
a.Max(b.Min(c)) == a.Max(b).Min(a.Max(c))
a.Min(b.Max(c)) == a.Min(b).Max(a.Min(c))
0
is the bottom element0.Min(a) == 0
0.Max(a) == a
For Admin Use
- Not duplicate issue
- Appropriate labels applied
- Appropriate contributors tagged
- Contributor assigned/self-assigned
Activity