diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-23 23:28:17 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-23 23:28:17 +0300 |
| commit | 26d4eb8d02f669a7526ff981fc184f902798b118 (patch) | |
| tree | c91a7e0e36783ed70d19de2e3e0a3ba7bc209292 | |
| parent | c1c9804db06e01efceef032347768e218d50f497 (diff) | |
rpn: use clear() builtin instead of loop+delete
Replace the manual for-range-delete pattern with Go 1.21+ clear()
builtin in Variables.ClearVariables() and Constants.ClearConstants().
Also simplified ReloadBuiltInConstants() which used the same pattern.
clear() is cleaner, more idiomatic, and slightly faster for
clearing entire maps.
| -rw-r--r-- | internal/rpn/constants.go | 32 | ||||
| -rw-r--r-- | internal/rpn/variables.go | 4 |
2 files changed, 4 insertions, 32 deletions
diff --git a/internal/rpn/constants.go b/internal/rpn/constants.go index 4c629bd..320ff07 100644 --- a/internal/rpn/constants.go +++ b/internal/rpn/constants.go @@ -147,20 +147,8 @@ func (c *Constants) ClearConstants() { c.mu.Lock() defer c.mu.Unlock() - // Remove only user-defined constants (not built-in ones) - builtIns := map[string]bool{ - "pi": true, "π": true, - "e": true, "euler": true, - "phi": true, "φ": true, - "sqrt2": true, "√2": true, - "inf": true, "infinity": true, - "nan": true, - } - for k := range c.constants { - if !builtIns[k] { - delete(c.constants, k) - } - } + clear(c.constants) + c.loadBuiltInConstants() } // ReloadBuiltInConstants restores all built-in constants. @@ -170,21 +158,7 @@ func (c *Constants) ReloadBuiltInConstants() { c.mu.Lock() defer c.mu.Unlock() - // First remove only user-defined constants - builtIns := map[string]bool{ - "pi": true, "π": true, - "e": true, "euler": true, - "phi": true, "φ": true, - "sqrt2": true, "√2": true, - "inf": true, "infinity": true, - "nan": true, - } - for k := range c.constants { - if !builtIns[k] { - delete(c.constants, k) - } - } - // Then reload built-in constants + clear(c.constants) c.loadBuiltInConstants() } diff --git a/internal/rpn/variables.go b/internal/rpn/variables.go index efc2a8d..7e6b209 100644 --- a/internal/rpn/variables.go +++ b/internal/rpn/variables.go @@ -159,9 +159,7 @@ func (v *Variables) ClearVariables() { v.mu.Lock() defer v.mu.Unlock() - for k := range v.variables { - delete(v.variables, k) - } + clear(v.variables) } // formatVariablesUnsafe returns a list of variable info without acquiring a lock. |
