Repository: elliotchance/pie
Pull request #79 (2019-05-01 23:16:43)

added: Each
Each is more condensed version of Transform that allows an action to happen on each elements and pass the original slice on.

<!-- 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/79)
<!-- Reviewable:end -->


codecov-io: # [Codecov](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=h1) Report
> Merging [#79](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=desc) into [master](https://codecov.io/gh/elliotchance/pie/commit/1dc63dc744e3812fe15a6103a981602b0d5cd3cf?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/elliotchance/pie/pull/79/graphs/tree.svg?width=650&token=OvugwUQGM3&height=150&src=pr)](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master    #79   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           7      7           
  Lines         670    690   +20     
=====================================
+ Hits          670    690   +20
```


| [Impacted Files](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pie/float64s\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/79/diff?src=pr&el=tree#diff-cGllL2Zsb2F0NjRzX3BpZS5nbw==) | `100% <100%> (ø)` | :arrow_up: |
| [pie/carpointers\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/79/diff?src=pr&el=tree#diff-cGllL2NhcnBvaW50ZXJzX3BpZS5nbw==) | `100% <100%> (ø)` | :arrow_up: |
| [pie/cars\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/79/diff?src=pr&el=tree#diff-cGllL2NhcnNfcGllLmdv) | `100% <100%> (ø)` | :arrow_up: |
| [pie/ints\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/79/diff?src=pr&el=tree#diff-cGllL2ludHNfcGllLmdv) | `100% <100%> (ø)` | :arrow_up: |
| [pie/strings\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/79/diff?src=pr&el=tree#diff-cGllL3N0cmluZ3NfcGllLmdv) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=footer). Last update [1dc63dc...0035a41](https://codecov.io/gh/elliotchance/pie/pull/79?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index 2a1169c..4cfaed2 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@
 focuses on type safety, performance and immutability.
 
 - [Quick Start](#quick-start)
+  * [Install/Update](#install-update)
   * [Built-in Types](#built-in-types)
   * [Custom Types](#custom-types)
   * [Limiting Functions Generated](#limiting-functions-generated)
@@ -19,7 +20,7 @@ focuses on type safety, performance and immutability.
 
 # Quick Start
 
-Install/update:
+## Install/Update
 
 ```bash
 go get -u github.com/elliotchance/pie
@@ -132,6 +133,7 @@ This will only generate `myInts.Average`, `myInts.Sum` and `myStrings.Select`.
 | `Bottom`     | ✓      | ✓      | ✓     |      | n        | Gets n elements from bottom. |
 | `Contains`   | ✓      | ✓      | ✓     |      | n        | Check if the value exists in the slice. |
 | `Extend`     | ✓      | ✓      | ✓     |      | n        | A new slice with the elements from each slice appended to the end. |
+| `Each`       | ✓      | ✓      | ✓     |      | n        | Perform an action on each element. |
 | `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. |
diff --git a/functions/each.go b/functions/each.go
new file mode 100644
index 0000000..286748f
--- /dev/null
+++ b/functions/each.go
@@ -0,0 +1,24 @@
+package functions
+
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss SliceType) Each(fn func(ElementType)) SliceType {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
diff --git a/functions/main.go b/functions/main.go
index c74d559..d0348c0 100644
--- a/functions/main.go
+++ b/functions/main.go
@@ -26,6 +26,7 @@ var Functions = []struct {
 	{"Average", "average.go", ForNumbers},
 	{"Bottom", "bottom.go", ForAll},
 	{"Contains", "contains.go", ForAll},
+	{"Each", "each.go", ForAll},
 	{"Extend", "extend.go", ForAll},
 	{"First", "first.go", ForAll},
 	{"FirstOr", "first_or.go", ForAll},
diff --git a/go.mod b/go.mod
index 545688c..dfc41da 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module github.com/elliotchance/pie
 
 go 1.12
 
-require github.com/stretchr/testify v1.3.0
+require (
+	github.com/davecgh/go-spew v1.1.1 // indirect
+	github.com/stretchr/testify v1.3.0
+)
diff --git a/go.sum b/go.sum
index 4347755..4f76e62 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,7 @@
 github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
diff --git a/pie/carpointers_pie.go b/pie/carpointers_pie.go
index f32c967..b5f65f1 100755
--- a/pie/carpointers_pie.go
+++ b/pie/carpointers_pie.go
@@ -72,6 +72,29 @@ func (ss carPointers) Contains(lookingFor *car) bool {
 	return false
 }
 
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss carPointers) Each(fn func(*car)) carPointers {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
+
 // Extend will return a new slice with the slices of elements appended to the
 // end.
 //
diff --git a/pie/cars_pie.go b/pie/cars_pie.go
index f9554d9..1005065 100755
--- a/pie/cars_pie.go
+++ b/pie/cars_pie.go
@@ -72,6 +72,29 @@ func (ss cars) Contains(lookingFor car) bool {
 	return false
 }
 
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss cars) Each(fn func(car)) cars {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
+
 // Extend will return a new slice with the slices of elements appended to the
 // end.
 //
diff --git a/pie/float64s_pie.go b/pie/float64s_pie.go
index ce22133..0107a9b 100755
--- a/pie/float64s_pie.go
+++ b/pie/float64s_pie.go
@@ -97,6 +97,29 @@ func (ss Float64s) Contains(lookingFor float64) bool {
 	return false
 }
 
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss Float64s) Each(fn func(float64)) Float64s {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
+
 // Extend will return a new slice with the slices of elements appended to the
 // end.
 //
diff --git a/pie/ints_pie.go b/pie/ints_pie.go
index 70b23dd..5136695 100755
--- a/pie/ints_pie.go
+++ b/pie/ints_pie.go
@@ -97,6 +97,29 @@ func (ss Ints) Contains(lookingFor int) bool {
 	return false
 }
 
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss Ints) Each(fn func(int)) Ints {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
+
 // Extend will return a new slice with the slices of elements appended to the
 // end.
 //
diff --git a/pie/strings_pie.go b/pie/strings_pie.go
index 2a17170..5732cf7 100755
--- a/pie/strings_pie.go
+++ b/pie/strings_pie.go
@@ -87,6 +87,29 @@ func (ss Strings) Contains(lookingFor string) bool {
 	return false
 }
 
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss Strings) Each(fn func(string)) Strings {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
+
 // Extend will return a new slice with the slices of elements appended to the
 // end.
 //
diff --git a/template.go b/template.go
index f79b9a6..97d728d 100644
--- a/template.go
+++ b/template.go
@@ -111,6 +111,31 @@ func (ss SliceType) Contains(lookingFor ElementType) bool {
 
 	return false
 }
+`,
+	"Each": `package functions
+
+// Each is more condensed version of Transform that allows an action to happen
+// on each elements and pass the original slice on.
+//
+//   cars.Each(func (car *Car) {
+//       fmt.Printf("Car color is: %s\n", car.Color)
+//   })
+//
+// Pie will not ensure immutability on items passed in so they can be
+// manipulated, if you choose to do it this way, for example:
+//
+//   // Set all car colors to Red.
+//   cars.Each(func (car *Car) {
+//       car.Color = "Red"
+//   })
+//
+func (ss SliceType) Each(fn func(ElementType)) SliceType {
+	for _, s := range ss {
+		fn(s)
+	}
+
+	return ss
+}
 `,
 	"Extend": `package functions
 


Test patch:
diff --git a/pie/carpointers_test.go b/pie/carpointers_test.go
index 2ab370c..f78a91a 100644
--- a/pie/carpointers_test.go
+++ b/pie/carpointers_test.go
@@ -560,3 +560,25 @@ func TestCarPointers_Bottom(t *testing.T) {
 		})
 	}
 }
+
+func TestCarPointers_Each(t *testing.T) {
+	var names []string
+
+	names = []string{}
+	carPointers(nil).Each(func(car *car) {
+		names = append(names, car.Name)
+	})
+	assert.Equal(t, []string{}, names)
+
+	names = []string{}
+	carPointers{}.Each(func(car *car) {
+		names = append(names, car.Name)
+	})
+	assert.Equal(t, []string{}, names)
+
+	names = []string{}
+	carPointers{&car{"bar", "yellow"}, &car{"Baz", "black"}}.Each(func(car *car) {
+		names = append(names, car.Name)
+	})
+	assert.Equal(t, []string{"bar", "Baz"}, names)
+}
diff --git a/pie/cars_test.go b/pie/cars_test.go
index 5278b1a..1d7024b 100644
--- a/pie/cars_test.go
+++ b/pie/cars_test.go
@@ -550,3 +550,19 @@ func TestCars_Bottom(t *testing.T) {
 		})
 	}
 }
+
+func TestCars_Each(t *testing.T) {
+	var names []string
+
+	names = []string{}
+	cars{}.Each(func(car car) {
+		names = append(names, car.Name)
+	})
+	assert.Equal(t, []string{}, names)
+
+	names = []string{}
+	cars{car{"bar", "yellow"}, car{"Baz", "black"}}.Each(func(car car) {
+		names = append(names, car.Name)
+	})
+	assert.Equal(t, []string{"bar", "Baz"}, names)
+}
diff --git a/pie/float64s_test.go b/pie/float64s_test.go
index 603df8f..ef8f8bb 100644
--- a/pie/float64s_test.go
+++ b/pie/float64s_test.go
@@ -669,3 +669,19 @@ func TestFloat64s_Median(t *testing.T) {
 	assert.Equal(t, 8.4, Float64s{12.3, 4.5}.Median())
 	assert.Equal(t, 4.5, Float64s{2.1, 12.3, 4.5}.Median())
 }
+
+func TestFloat64s_Each(t *testing.T) {
+	var values []float64
+
+	values = []float64{}
+	Float64s{}.Each(func(value float64) {
+		values = append(values, value)
+	})
+	assert.Equal(t, []float64{}, values)
+
+	values = []float64{}
+	Float64s{435.34, 8923.1}.Each(func(value float64) {
+		values = append(values, value)
+	})
+	assert.Equal(t, []float64{435.34, 8923.1}, values)
+}
diff --git a/pie/ints_test.go b/pie/ints_test.go
index 1943cca..7edd2be 100644
--- a/pie/ints_test.go
+++ b/pie/ints_test.go
@@ -655,3 +655,19 @@ func TestInts_Median(t *testing.T) {
 	assert.Equal(t, 8, Ints{12, 4}.Median())
 	assert.Equal(t, 4, Ints{2, 12, 4}.Median())
 }
+
+func TestInts_Each(t *testing.T) {
+	var values []int
+
+	values = []int{}
+	Ints{}.Each(func(value int) {
+		values = append(values, value)
+	})
+	assert.Equal(t, []int{}, values)
+
+	values = []int{}
+	Ints{435, 8923}.Each(func(value int) {
+		values = append(values, value)
+	})
+	assert.Equal(t, []int{435, 8923}, values)
+}
diff --git a/pie/strings_test.go b/pie/strings_test.go
index 17a62d9..f9673c2 100644
--- a/pie/strings_test.go
+++ b/pie/strings_test.go
@@ -641,3 +641,19 @@ func TestStrings_Bottom(t *testing.T) {
 		})
 	}
 }
+
+func TestStrings_Each(t *testing.T) {
+	var values []string
+
+	values = []string{}
+	Strings{}.Each(func(value string) {
+		values = append(values, value)
+	})
+	assert.Equal(t, []string{}, values)
+
+	values = []string{}
+	Strings{"baz", "qux"}.Each(func(value string) {
+		values = append(values, value)
+	})
+	assert.Equal(t, []string{"baz", "qux"}, values)
+}