more variants of the Partial function, #235 (#236)

* more variants of the Partial function, #235

* add comment for Partial1 func
This commit is contained in:
Ziyadin Shemsedinov
2022-12-09 18:52:11 +04:00
committed by GitHub
parent c729db34d0
commit 510d0ffaac
2 changed files with 93 additions and 0 deletions

33
func.go
View File

@@ -6,3 +6,36 @@ func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R {
return f(arg1, t2)
}
}
// Partial1 returns new function that, when called, has its first argument set to the provided value.
func Partial1[T1, T2, R any](f func(T1, T2) R, arg1 T1) func(T2) R {
return Partial(f, arg1)
}
// Partial2 returns new function that, when called, has its first argument set to the provided value.
func Partial2[T1, T2, T3, R any](f func(T1, T2, T3) R, arg1 T1) func(T2, T3) R {
return func(t2 T2, t3 T3) R {
return f(arg1, t2, t3)
}
}
// Partial3 returns new function that, when called, has its first argument set to the provided value.
func Partial3[T1, T2, T3, T4, R any](f func(T1, T2, T3, T4) R, arg1 T1) func(T2, T3, T4) R {
return func(t2 T2, t3 T3, t4 T4) R {
return f(arg1, t2, t3, t4)
}
}
// Partial4 returns new function that, when called, has its first argument set to the provided value.
func Partial4[T1, T2, T3, T4, T5, R any](f func(T1, T2, T3, T4, T5) R, arg1 T1) func(T2, T3, T4, T5) R {
return func(t2 T2, t3 T3, t4 T4, t5 T5) R {
return f(arg1, t2, t3, t4, t5)
}
}
// Partial5 returns new function that, when called, has its first argument set to the provided value
func Partial5[T1, T2, T3, T4, T5, T6, R any](f func(T1, T2, T3, T4, T5, T6) R, arg1 T1) func(T2, T3, T4, T5, T6) R {
return func(t2 T2, t3 T3, t4 T4, t5 T5, t6 T6) R {
return f(arg1, t2, t3, t4, t5, t6)
}
}

View File

@@ -18,3 +18,63 @@ func TestPartial(t *testing.T) {
is.Equal("15", f(10))
is.Equal("0", f(-5))
}
func TestPartial1(t *testing.T) {
t.Parallel()
is := assert.New(t)
add := func(x float64, y int) string {
return strconv.Itoa(int(x) + y)
}
f := Partial1(add, 5)
is.Equal("15", f(10))
is.Equal("0", f(-5))
}
func TestPartial2(t *testing.T) {
t.Parallel()
is := assert.New(t)
add := func(x float64, y int, z int) string {
return strconv.Itoa(int(x) + y + z)
}
f := Partial2(add, 5)
is.Equal("24", f(10, 9))
is.Equal("8", f(-5, 8))
}
func TestPartial3(t *testing.T) {
t.Parallel()
is := assert.New(t)
add := func(x float64, y int, z int, a float32) string {
return strconv.Itoa(int(x) + y + z + int(a))
}
f := Partial3(add, 5)
is.Equal("21", f(10, 9, -3))
is.Equal("15", f(-5, 8, 7))
}
func TestPartial4(t *testing.T) {
t.Parallel()
is := assert.New(t)
add := func(x float64, y int, z int, a float32, b int32) string {
return strconv.Itoa(int(x) + y + z + int(a) + int(b))
}
f := Partial4(add, 5)
is.Equal("21", f(10, 9, -3, 0))
is.Equal("14", f(-5, 8, 7, -1))
}
func TestPartial5(t *testing.T) {
t.Parallel()
is := assert.New(t)
add := func(x float64, y int, z int, a float32, b int32, c int) string {
return strconv.Itoa(int(x) + y + z + int(a) + int(b) + c)
}
f := Partial5(add, 5)
is.Equal("26", f(10, 9, -3, 0, 5))
is.Equal("21", f(-5, 8, 7, -1, 7))
}