From 26d4eb8d02f669a7526ff981fc184f902798b118 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 23 May 2026 23:28:17 +0300 Subject: 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. --- internal/rpn/constants.go | 32 +++----------------------------- 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. -- cgit v1.2.3