Repository: elliotchance/pie
Pull request #52 (2019-04-12 01:06:29)

Adding Join()
Join returns a string from joining each of the elements.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/pie/52)
<!-- Reviewable:end -->



Patch:
diff --git a/README.md b/README.md
index 0615081..3cad33b 100644
--- a/README.md
+++ b/README.md
@@ -126,6 +126,7 @@ This will only generate `myInts.Average`, `myInts.Sum` and `myStrings.Select`.
 | `Contains`   | ✓      | ✓      | ✓      | n        | Check if the value exists in the slice. |
 | `First`      | ✓      | ✓      | ✓      | 1        | The first element, or a zeroed value. |
 | `FirstOr`    | ✓      | ✓      | ✓      | 1        | The first element, or a default value. |
+| `Join`       | ✓      |        |        | n        | A string from joining each of the elements. |
 | `JSONString` | ✓      | ✓      | ✓      | n        | The JSON encoded string. |
 | `Last`       | ✓      | ✓      | ✓      | 1        | The last element, or a zeroed value. |
 | `LastOr`     | ✓      | ✓      | ✓      | 1        | The last element, or a default value. |
diff --git a/generate_template.go b/generate_template.go
index 679f64a..ba08d60 100644
--- a/generate_template.go
+++ b/generate_template.go
@@ -9,13 +9,14 @@ import (
 )
 
 var packageTemplate = template.Must(template.New("").
-	Parse("// Code generated by go generate; DO NOT EDIT.\npackage main\n\nvar pieAllTemplate = `{{ .All }}`\n\nvar pieStringsNumbersTemplate = `{{ .StringsNumbers }}`\n\nvar pieNumbersTemplate = `{{ .Numbers }}`\n"))
+	Parse("// Code generated by go generate; DO NOT EDIT.\npackage main\n\nvar pieAllTemplate = `{{ .All }}`\n\nvar pieStringsNumbersTemplate = `{{ .StringsNumbers }}`\n\nvar pieNumbersTemplate = `{{ .Numbers }}`\n\nvar pieStringsTemplate = `{{ .Strings }}`\n"))
 
 func main() {
 	files := map[string]string{
 		"All":            "pie_all.go",
 		"StringsNumbers": "pie_strings_numbers.go",
 		"Numbers":        "pie_numbers.go",
+		"Strings":        "pie_strings.go",
 	}
 	data := map[string]string{}
 
diff --git a/main.go b/main.go
index 44edc47..993ef1f 100644
--- a/main.go
+++ b/main.go
@@ -18,6 +18,8 @@ import (
 
 type ElementType float64
 type SliceType []ElementType
+type StringElementType string
+type StringSliceType []StringElementType
 
 var ElementZeroValue ElementType
 
@@ -144,6 +146,10 @@ func main() {
 			templates = append(templates, pieNumbersTemplate)
 		}
 
+		if kind == "string" {
+			templates = append(templates, pieStringsTemplate)
+		}
+
 		// Aggregate imports.
 		t := fmt.Sprintf("package %s\n\nimport (", packageName)
 		for _, imp := range getAllImports(packageName, templates) {
@@ -156,6 +162,8 @@ func main() {
 			t += tmpl[i:] + "\n"
 		}
 
+		t = strings.Replace(t, "StringSliceType", sliceType, -1)
+		t = strings.Replace(t, "StringElementType", elementType, -1)
 		t = strings.Replace(t, "SliceType", sliceType, -1)
 		t = strings.Replace(t, "ElementType", elementType, -1)
 
diff --git a/pie/strings_pie.go b/pie/strings_pie.go
index 4b90ccc..42076f2 100755
--- a/pie/strings_pie.go
+++ b/pie/strings_pie.go
@@ -189,3 +189,15 @@ func (ss Strings) Unique() Strings {
 
 	return uniqueValues
 }
+
+func (ss Strings) Join(glue string) (s string) {
+	for i, element := range ss {
+		if i > 0 {
+			s += glue
+		}
+
+		s += string(element)
+	}
+
+	return s
+}
diff --git a/pie_strings.go b/pie_strings.go
new file mode 100644
index 0000000..27ba4dd
--- /dev/null
+++ b/pie_strings.go
@@ -0,0 +1,16 @@
+package main
+
+// The functions in this file only work on string slices.
+
+// Join returns a string from joining each of the elements.
+func (ss StringSliceType) Join(glue string) (s string) {
+	for i, element := range ss {
+		if i > 0 {
+			s += glue
+		}
+
+		s += string(element)
+	}
+
+	return s
+}
diff --git a/template.go b/template.go
index 0a50327..c397a9b 100644
--- a/template.go
+++ b/template.go
@@ -285,3 +285,21 @@ func (ss SliceType) Sum() (sum ElementType) {
 	return
 }
 `
+
+var pieStringsTemplate = `package main
+
+// The functions in this file only work on string slices.
+
+// Join returns a string from joining each of the elements.
+func (ss StringSliceType) Join(glue string) (s string) {
+	for i, element := range ss {
+		if i > 0 {
+			s += glue
+		}
+
+		s += string(element)
+	}
+
+	return s
+}
+`


Test patch:
diff --git a/pie/strings_test.go b/pie/strings_test.go
index 0377fb6..33271f7 100644
--- a/pie/strings_test.go
+++ b/pie/strings_test.go
@@ -406,3 +406,9 @@ func TestStrings_ToStrings(t *testing.T) {
 		})
 	}
 }
+
+func TestStrings_Join(t *testing.T) {
+	assert.Equal(t, "", Strings(nil).Join("a"))
+	assert.Equal(t, "", Strings{}.Join("a"))
+	assert.Equal(t, "foo--bar", Strings{"foo", "", "bar"}.Join("-"))
+}