Skip to content

Commit 963dcce

Browse files
committed
strings: update
1 parent d68cee1 commit 963dcce

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

strings/strings.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

strings/strings.go2

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,51 @@
44

55
package strings
66

7-
// An interface specifies methods and types for each of the
8-
// type parameters it constrains.
7+
// Stringer is a type constraint that requires the type argument to have
8+
// a String method and permits the generic function to call String.
9+
// The String method should return a string reqpresentation of the value.
910
type Stringer interface {
1011
String() string
1112
}
1213

13-
14-
func Stringify[type T Stringer](s []T) ([]string) {
15-
ret := []string{}
14+
// Stringify calls the String method on each element of s,
15+
// and returns the results.
16+
func Stringify[type T Stringer](s []T) (ret []string) {
1617
for _, v := range s {
1718
ret = append(ret, v.String())
1819
}
19-
return ret
20+
return
21+
}
22+
23+
// Stringify2 converts two slices of different types to strings,
24+
// and returns the concatenation of all the strings.
25+
func Stringify2[type T1, T2 Stringer](s1 []T1, s2 []T2) string {
26+
r := ""
27+
for _, v1 := range s1 {
28+
r += v1.String()
29+
}
30+
for _, v2 := range s2 {
31+
r += v2.String()
32+
}
33+
return r
34+
}
35+
36+
// Plusser is a type constraint that requires a Plus method.
37+
// The Plus method is expected to add the argument to an internal
38+
// string and return the result.
39+
type Plusser interface {
40+
Plus(string) string
41+
}
42+
43+
// ConcatTo takes a slice of elements with a String method and a slice
44+
// of elements with a Plus method. The slices should have the same
45+
// number of elements. This will convert each element of s to a string,
46+
// pass it to the Plus method of the corresponding element of p,
47+
// and return a slice of the resulting strings.
48+
func ConcatTo[type S Stringer, P Plusser](s []S, p []P) []string {
49+
r := make([]string, len(s))
50+
for i, v := range s {
51+
r[i] = p[i].Plus(v.String())
52+
}
53+
return r
2054
}

strings/strings_test.go

Lines changed: 13 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)