From a73df8c1d9cfafef8dc248ef3ba67769e79b125b Mon Sep 17 00:00:00 2001 From: jpfourny Date: Wed, 10 Jan 2024 22:22:12 -0500 Subject: [PATCH] Move Increment/Decrement from math.go into misc.go. --- pkg/mapper/math.go | 17 ----------------- pkg/mapper/math_test.go | 21 --------------------- pkg/mapper/misc.go | 16 ++++++++++++++++ pkg/mapper/misc_test.go | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 38 deletions(-) delete mode 100644 pkg/mapper/math.go delete mode 100644 pkg/mapper/math_test.go diff --git a/pkg/mapper/math.go b/pkg/mapper/math.go deleted file mode 100644 index 2f2a01b..0000000 --- a/pkg/mapper/math.go +++ /dev/null @@ -1,17 +0,0 @@ -package mapper - -import "github.com/jpfourny/papaya/pkg/constraint" - -// Increment returns a function that accepts a value of real number type E and returns the result of adding the provided `step` value to it. -func Increment[E constraint.RealNumber](step E) func(E) E { - return func(e E) E { - return e + step - } -} - -// Decrement returns a function that accepts a value of real number type E and returns the result of subtracting the provided `step` value from it. -func Decrement[E constraint.RealNumber](step E) func(E) E { - return func(e E) E { - return e - step - } -} diff --git a/pkg/mapper/math_test.go b/pkg/mapper/math_test.go deleted file mode 100644 index 94fd2e9..0000000 --- a/pkg/mapper/math_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package mapper - -import "testing" - -func TestIncrement(t *testing.T) { - m := Increment[int](2) - got := m(42) - want := 44 - if got != want { - t.Errorf("Increment(2)(42) = %#v; want %#v", got, want) - } -} - -func TestDecrement(t *testing.T) { - m := Decrement[int](2) - got := m(42) - want := 40 - if got != want { - t.Errorf("Decrement(2)(42) = %#v; want %#v", got, want) - } -} diff --git a/pkg/mapper/misc.go b/pkg/mapper/misc.go index 8c7ddb7..bf9494c 100644 --- a/pkg/mapper/misc.go +++ b/pkg/mapper/misc.go @@ -1,5 +1,7 @@ package mapper +import "github.com/jpfourny/papaya/pkg/constraint" + // Identity returns a function that accepts a value of any type E and returns that value. func Identity[E any]() func(E) E { return func(e E) E { @@ -13,3 +15,17 @@ func Constant[E any, F any](c F) func(E) F { return c } } + +// Increment returns a function that accepts a value of real number type E and returns the result of adding the provided `step` value to it. +func Increment[E constraint.RealNumber](step E) func(E) E { + return func(e E) E { + return e + step + } +} + +// Decrement returns a function that accepts a value of real number type E and returns the result of subtracting the provided `step` value from it. +func Decrement[E constraint.RealNumber](step E) func(E) E { + return func(e E) E { + return e - step + } +} diff --git a/pkg/mapper/misc_test.go b/pkg/mapper/misc_test.go index 25d541f..bc577b9 100644 --- a/pkg/mapper/misc_test.go +++ b/pkg/mapper/misc_test.go @@ -21,3 +21,21 @@ func TestIdentity(t *testing.T) { t.Errorf("Identity()(42) = %#v; want %#v", got, want) } } + +func TestIncrement(t *testing.T) { + m := Increment[int](2) + got := m(42) + want := 44 + if got != want { + t.Errorf("Increment(2)(42) = %#v; want %#v", got, want) + } +} + +func TestDecrement(t *testing.T) { + m := Decrement[int](2) + got := m(42) + want := 40 + if got != want { + t.Errorf("Decrement(2)(42) = %#v; want %#v", got, want) + } +}