diff options
| author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-21 08:24:32 -0500 |
|---|---|---|
| committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2023-08-21 09:56:06 -0500 |
| commit | 30e92c33303535a86e56824b668f46ad0c6261a8 (patch) | |
| tree | df037059d07f48d1bd4ad434bf81b6e6688a032a /factors | |
| parent | ff5bc533399cb7fc7b83afcc3a20e6abbc05d444 (diff) | |
Upgrade to Go 1.21
Use methods from the new slices and maps modules to make my code faster
and more compact.
Diffstat (limited to 'factors')
| -rw-r--r-- | factors/factors_test.go | 31 | ||||
| -rw-r--r-- | factors/prime_factorization.go | 4 |
2 files changed, 11 insertions, 24 deletions
diff --git a/factors/factors_test.go b/factors/factors_test.go index 0029d67..77c5f4a 100644 --- a/factors/factors_test.go +++ b/factors/factors_test.go @@ -2,13 +2,14 @@ package factors import ( "fmt" + "maps" "math" "testing" ) // Run a test with a table of cases func tableTest[IN comparable, OUT any](t *testing.T, toTest func(IN) OUT, - cases map[IN]OUT, equal func(OUT, OUT) bool, name string) { + cases map[IN]OUT, equal func(OUT, OUT) bool, name string) { for input, expected := range cases { t.Run(fmt.Sprintf("%v", input), func(t *testing.T) { actual := toTest(input) @@ -19,9 +20,6 @@ func tableTest[IN comparable, OUT any](t *testing.T, toTest func(IN) OUT, } } -// to be used as the equal paramater for tableTest -func stdEquals[T comparable](a, b T) bool { return a == b } - var primeFactorCases = map[uint]PrimeFactorization{ 0: PrimeFactorization{map[uint]uint{0: 1}}, 1: PrimeFactorization{map[uint]uint{}}, @@ -38,7 +36,7 @@ var primeFactorCases = map[uint]PrimeFactorization{ func TestPrimeFactorize(t *testing.T) { equal := func(a, b PrimeFactorization) bool { - return mapEquals(a.exponents, b.exponents) + return maps.Equal(a.exponents, b.exponents) } tableTest(t, PrimeFactorize, primeFactorCases, equal, "PrimeFactorize") } @@ -102,32 +100,21 @@ func TestBasicRank(t *testing.T) { tableTest(t, BasicRank, basicRankCases, stdEquals[string], "BasicRank") } -func mapEquals[K comparable, V comparable](a, b map[K]V) bool { - for k := range a { - if a[k] != b[k] { - return false - } - } - for k := range b { - if a[k] != b[k] { - return false - } - } - return true -} +// to be used as the equal paramater for tableTest +func stdEquals[T comparable](a, b T) bool { return a == b } -func setEquals(a, b []uint) bool { +func setEquals[E comparable](a, b []E) bool { // use maps to simulate sets // aSet[a] == true means set contains a, false means not - aSet := make(map[uint]bool) - bSet := make(map[uint]bool) + aSet := make(map[E]bool) + bSet := make(map[E]bool) for _, i := range a { aSet[i] = true } for _, j := range b { bSet[j] = true } - return mapEquals(aSet, bSet) + return maps.Equal(aSet, bSet) } func floatEquals(a, b, maxDelta float64) bool { diff --git a/factors/prime_factorization.go b/factors/prime_factorization.go index 3f3abc7..69d0e08 100644 --- a/factors/prime_factorization.go +++ b/factors/prime_factorization.go @@ -2,7 +2,7 @@ package factors import ( "fmt" - "sort" + "slices" "strings" ) @@ -67,7 +67,7 @@ func (factors PrimeFactorization) String() string { for p := range factors.exponents { primes = append(primes, int(p)) } - sort.Ints(primes) + slices.Sort(primes) for _, p := range primes { if factors.Exponent(uint(p)) == 1 { |
