summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-21 08:24:32 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-08-21 09:56:06 -0500
commit30e92c33303535a86e56824b668f46ad0c6261a8 (patch)
treedf037059d07f48d1bd4ad434bf81b6e6688a032a
parentff5bc533399cb7fc7b83afcc3a20e6abbc05d444 (diff)
Upgrade to Go 1.21
Use methods from the new slices and maps modules to make my code faster and more compact.
-rw-r--r--factors/factors_test.go31
-rw-r--r--factors/prime_factorization.go4
-rw-r--r--go.mod2
-rw-r--r--radix_info.go6
4 files changed, 14 insertions, 29 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 {
diff --git a/go.mod b/go.mod
index 857b6d4..3e9c6d8 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,3 @@
module aphopkins/radix_info
-go 1.20
+go 1.21
diff --git a/radix_info.go b/radix_info.go
index 3e83aad..a107b73 100644
--- a/radix_info.go
+++ b/radix_info.go
@@ -4,7 +4,7 @@ import (
"aphopkins/radix_info/factors"
"fmt"
"os"
- "sort"
+ "slices"
"strconv"
)
@@ -15,9 +15,7 @@ func main() {
n := uint(n)
fmt.Printf("%d = %s\n", n, factors.PrimeFactorize(n))
n_factors := factors.Factors(n)
- sort.Slice(n_factors, func(i, j int) bool {
- return n_factors[i] < n_factors[j]
- })
+ slices.Sort(n_factors)
factorScore := factors.Score(n)
fmt.Printf("Factors: %v (Score: %.2f)\n", n_factors, factorScore)
fmt.Printf("Totative Ratio: %03.1f%%\n",