Skip to content

Commit

Permalink
Move Increment/Decrement from math.go into misc.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfourny committed Jan 11, 2024
1 parent addaee3 commit a73df8c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 38 deletions.
17 changes: 0 additions & 17 deletions pkg/mapper/math.go

This file was deleted.

21 changes: 0 additions & 21 deletions pkg/mapper/math_test.go

This file was deleted.

16 changes: 16 additions & 0 deletions pkg/mapper/misc.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
}
18 changes: 18 additions & 0 deletions pkg/mapper/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit a73df8c

Please sign in to comment.