1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

feat: add ConcatBy

This commit is contained in:
dudaodong
2024-10-24 15:38:12 +08:00
parent 2015d36b08
commit a4e89bd7c1
5 changed files with 184 additions and 0 deletions

View File

@@ -1411,3 +1411,22 @@ func JoinFunc[T any](slice []T, sep string, transform func(T) T) string {
}
return buf.String()
}
// ConcatBy concats the elements of a slice into a single value using the provided separator and connector function.
// Play: todo
func ConcatBy[T any](slice []T, sep T, connector func(T, T) T) T {
var result T
if len(slice) == 0 {
return result
}
for i, v := range slice {
result = connector(result, v)
if i < len(slice)-1 {
result = connector(result, sep)
}
}
return result
}