Skip to content

Commit

Permalink
feat(func): add FuncWithIndex and FuncWithIndexNoReturn
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael DEMACON <[email protected]>
  • Loading branch information
quantumsheep committed Nov 28, 2024
1 parent efef0ff commit 7b493bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ func Partial5[T1, T2, T3, T4, T5, T6, R any](f func(T1, T2, T3, T4, T5, T6) R, a
return f(arg1, t2, t3, t4, t5, t6)
}
}

func FuncWithIndexNoReturn[T any](f func(T)) func(T, int) {
return func(v T, _ int) {
f(v)
}
}

func FuncWithIndex[T any, U any](f func(T) U) func(T, int) U {
return func(v T, _ int) U {
return f(v)
}
}
22 changes: 22 additions & 0 deletions func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,25 @@ func TestPartial5(t *testing.T) {
is.Equal("26", f(10, 9, -3, 0, 5))
is.Equal("21", f(-5, 8, 7, -1, 7))
}

func TestFuncWithIndexNoReturn(t *testing.T) {
t.Parallel()
is := assert.New(t)

var result string
f := FuncWithIndexNoReturn(func(x string) {
result = x
})
f("lo", 0)
is.Equal("lo", result)
}

func TestFuncWithIndex(t *testing.T) {
t.Parallel()
is := assert.New(t)

f := FuncWithIndex(func(x string) string {
return x
})
is.Equal("lo", f("lo", 0))
}

0 comments on commit 7b493bc

Please sign in to comment.