Repository: elliotchance/pie
Pull request #121 (2019-05-09 07:42:37)

added: Ints and Floats64s
Ints and Floats64s transforms each element to an int or float64 respectively.

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



Patch:
diff --git a/README.md b/README.md
index e5c68d5..cd9c4f3 100644
--- a/README.md
+++ b/README.md
@@ -189,7 +189,9 @@ The letters in brackets indicate:
 | `FilterNot`       | ✓      | ✓      | ✓      |      | n        | A new slice containing only the elements that returned false from the condition. |
 | `First`           | ✓      | ✓      | ✓      |      | 1        | The first element, or a zeroed value. |
 | `FirstOr`         | ✓      | ✓      | ✓      |      | 1        | The first element, or a default value. |
+| `Float64s` (S)    | ✓      | ✓      | ✓      |      | n        | Transforms each element into a float64. |
 | `Intersect`       | ✓      | ✓      |        |      | n        | Intersect returns elements which exists in all slices. |
+| `Ints` (S)        | ✓      | ✓      | ✓      |      | n        | Transforms each element into an int. |
 | `Join`            | ✓      |        |        |      | n        | A string from joining each of the elements. |
 | `JSONString`      | ✓      | ✓      | ✓      |      | n        | The JSON encoded string. |
 | `Keys`            |        |        |        | ✓    | n        | Returns all keys in the map (in random order). |
diff --git a/functions/float64s.go b/functions/float64s.go
new file mode 100644
index 0000000..8ac1d7c
--- /dev/null
+++ b/functions/float64s.go
@@ -0,0 +1,24 @@
+package functions
+
+import (
+	"github.com/elliotchance/pie/pie"
+	"strconv"
+)
+
+// Float64s transforms each element to a float64.
+func (ss SliceType) Float64s() pie.Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(pie.Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(mightBeString.String(), 64)
+	}
+
+	return result
+}
diff --git a/functions/ints.go b/functions/ints.go
new file mode 100644
index 0000000..2cd95ce
--- /dev/null
+++ b/functions/ints.go
@@ -0,0 +1,25 @@
+package functions
+
+import (
+	"github.com/elliotchance/pie/pie"
+	"strconv"
+)
+
+// Ints transforms each element to an integer.
+func (ss SliceType) Ints() pie.Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(pie.Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(mightBeString.String(), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
diff --git a/functions/main.go b/functions/main.go
index ce09153..d36cd59 100644
--- a/functions/main.go
+++ b/functions/main.go
@@ -36,7 +36,9 @@ var Functions = []struct {
 	{"FilterNot", "filter_not.go", ForAll},
 	{"First", "first.go", ForAll},
 	{"FirstOr", "first_or.go", ForAll},
+	{"Float64s", "float64s.go", ForAll},
 	{"Intersect", "intersect.go", ForNumbersAndStrings},
+	{"Ints", "ints.go", ForAll},
 	{"Join", "join.go", ForStrings},
 	{"JSONString", "json_string.go", ForAll},
 	{"Keys", "keys.go", ForMaps},
diff --git a/main.go b/main.go
index e3769f9..78f9399 100644
--- a/main.go
+++ b/main.go
@@ -217,6 +217,8 @@ func main() {
 
 		if isSelfPackage(packageName) {
 			t = strings.Replace(t, "pie.Strings", "Strings", -1)
+			t = strings.Replace(t, "pie.Ints", "Ints", -1)
+			t = strings.Replace(t, "pie.Float64s", "Float64s", -1)
 		}
 
 		// The TrimRight is important to remove an extra new line that conflicts
diff --git a/pie/carpointers_pie.go b/pie/carpointers_pie.go
index a406ee1..89639d8 100755
--- a/pie/carpointers_pie.go
+++ b/pie/carpointers_pie.go
@@ -7,6 +7,7 @@ import (
 	"github.com/elliotchance/pie/pie/util"
 	"math/rand"
 	"sort"
+	"strconv"
 )
 
 // All will return true if all callbacks return true. It follows the same logic
@@ -196,6 +197,43 @@ func (ss carPointers) FirstOr(defaultValue *car) *car {
 	return ss[0]
 }
 
+// Float64s transforms each element to a float64.
+func (ss carPointers) Float64s() Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+	}
+
+	return result
+}
+
+// Ints transforms each element to an integer.
+func (ss carPointers) Ints() Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/cars_pie.go b/pie/cars_pie.go
index 5701142..6d39f38 100755
--- a/pie/cars_pie.go
+++ b/pie/cars_pie.go
@@ -7,6 +7,7 @@ import (
 	"github.com/elliotchance/pie/pie/util"
 	"math/rand"
 	"sort"
+	"strconv"
 )
 
 // All will return true if all callbacks return true. It follows the same logic
@@ -196,6 +197,43 @@ func (ss cars) FirstOr(defaultValue car) car {
 	return ss[0]
 }
 
+// Float64s transforms each element to a float64.
+func (ss cars) Float64s() Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+	}
+
+	return result
+}
+
+// Ints transforms each element to an integer.
+func (ss cars) Ints() Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/float64s_pie.go b/pie/float64s_pie.go
index ec268b5..1978f96 100755
--- a/pie/float64s_pie.go
+++ b/pie/float64s_pie.go
@@ -8,6 +8,7 @@ import (
 	"math"
 	"math/rand"
 	"sort"
+	"strconv"
 )
 
 // Abs is a function which returns the absolute value of all the
@@ -230,6 +231,24 @@ func (ss Float64s) FirstOr(defaultValue float64) float64 {
 	return ss[0]
 }
 
+// Float64s transforms each element to a float64.
+func (ss Float64s) Float64s() Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+	}
+
+	return result
+}
+
 // Intersect returns items that exist in all lists.
 //
 // It returns slice without any duplicates.
@@ -265,6 +284,25 @@ func (ss Float64s) Intersect(slices ...Float64s) (ss2 Float64s) {
 	return
 }
 
+// Ints transforms each element to an integer.
+func (ss Float64s) Ints() Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/ints_pie.go b/pie/ints_pie.go
index f503c92..8d7c763 100755
--- a/pie/ints_pie.go
+++ b/pie/ints_pie.go
@@ -8,6 +8,7 @@ import (
 	"math"
 	"math/rand"
 	"sort"
+	"strconv"
 )
 
 // Abs is a function which returns the absolute value of all the
@@ -230,6 +231,24 @@ func (ss Ints) FirstOr(defaultValue int) int {
 	return ss[0]
 }
 
+// Float64s transforms each element to a float64.
+func (ss Ints) Float64s() Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+	}
+
+	return result
+}
+
 // Intersect returns items that exist in all lists.
 //
 // It returns slice without any duplicates.
@@ -265,6 +284,25 @@ func (ss Ints) Intersect(slices ...Ints) (ss2 Ints) {
 	return
 }
 
+// Ints transforms each element to an integer.
+func (ss Ints) Ints() Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
+
 // JSONString returns the JSON encoded array as a string.
 //
 // One important thing to note is that it will treat a nil slice as an empty
diff --git a/pie/strings_pie.go b/pie/strings_pie.go
index fafd277..122cb00 100755
--- a/pie/strings_pie.go
+++ b/pie/strings_pie.go
@@ -7,6 +7,7 @@ import (
 	"github.com/elliotchance/pie/pie/util"
 	"math/rand"
 	"sort"
+	"strconv"
 )
 
 // All will return true if all callbacks return true. It follows the same logic
@@ -210,6 +211,24 @@ func (ss Strings) FirstOr(defaultValue string) string {
 	return ss[0]
 }
 
+// Float64s transforms each element to a float64.
+func (ss Strings) Float64s() Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+	}
+
+	return result
+}
+
 // Intersect returns items that exist in all lists.
 //
 // It returns slice without any duplicates.
@@ -245,6 +264,25 @@ func (ss Strings) Intersect(slices ...Strings) (ss2 Strings) {
 	return
 }
 
+// Ints transforms each element to an integer.
+func (ss Strings) Ints() Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(fmt.Sprintf("%v", mightBeString), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
+
 // Join returns a string from joining each of the elements.
 func (ss Strings) Join(glue string) (s string) {
 	for i, element := range ss {
diff --git a/template.go b/template.go
index e3bec15..5166f17 100644
--- a/template.go
+++ b/template.go
@@ -261,6 +261,31 @@ func (ss SliceType) FirstOr(defaultValue ElementType) ElementType {
 
 	return ss[0]
 }
+`,
+	"Float64s": `package functions
+
+import (
+	"github.com/elliotchance/pie/pie"
+	"strconv"
+)
+
+// Float64s transforms each element to a float64.
+func (ss SliceType) Float64s() pie.Float64s {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(pie.Float64s, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		result[i], _ = strconv.ParseFloat(mightBeString.String(), 64)
+	}
+
+	return result
+}
 `,
 	"Intersect": `package functions
 
@@ -298,6 +323,32 @@ func (ss SliceType) Intersect(slices ...SliceType) (ss2 SliceType) {
 
 	return
 }
+`,
+	"Ints": `package functions
+
+import (
+	"github.com/elliotchance/pie/pie"
+	"strconv"
+)
+
+// Ints transforms each element to an integer.
+func (ss SliceType) Ints() pie.Ints {
+	l := len(ss)
+
+	// Avoid the allocation.
+	if l == 0 {
+		return nil
+	}
+
+	result := make(pie.Ints, l)
+	for i := 0; i < l; i++ {
+		mightBeString := ss[i]
+		f, _ := strconv.ParseFloat(mightBeString.String(), 64)
+		result[i] = int(f)
+	}
+
+	return result
+}
 `,
 	"JSONString": `package functions
 


Test patch:
diff --git a/pie/carpointers_test.go b/pie/carpointers_test.go
index dda4f99..a1e62b4 100644
--- a/pie/carpointers_test.go
+++ b/pie/carpointers_test.go
@@ -826,3 +826,23 @@ func TestCarPointers_Strings(t *testing.T) {
 		Strings{"a is green", "b is blue", "c is gray"},
 		carPointers{carPointerA, carPointerB, carPointerC}.Strings())
 }
+
+func TestCarPointers_Ints(t *testing.T) {
+	assert.Equal(t, Ints(nil), carPointers(nil).Ints())
+
+	assert.Equal(t, Ints(nil), carPointers{}.Ints())
+
+	assert.Equal(t,
+		Ints{0, 0, 0},
+		carPointers{carPointerA, carPointerB, carPointerC}.Ints())
+}
+
+func TestCarPointers_Float64s(t *testing.T) {
+	assert.Equal(t, Float64s(nil), carPointers(nil).Float64s())
+
+	assert.Equal(t, Float64s(nil), carPointers{}.Float64s())
+
+	assert.Equal(t,
+		Float64s{0, 0, 0},
+		carPointers{carPointerA, carPointerB, carPointerC}.Float64s())
+}
diff --git a/pie/cars_test.go b/pie/cars_test.go
index 05f2086..98c19cf 100644
--- a/pie/cars_test.go
+++ b/pie/cars_test.go
@@ -805,3 +805,19 @@ func TestCars_Strings(t *testing.T) {
 		Strings{"{a green}", "{bar yellow}", "{Baz black}"},
 		cars{car{"a", "green"}, car{"bar", "yellow"}, car{"Baz", "black"}}.Strings())
 }
+
+func TestCars_Ints(t *testing.T) {
+	assert.Equal(t, Ints(nil), cars{}.Ints())
+
+	assert.Equal(t,
+		Ints{0, 0, 0},
+		cars{car{"a", "green"}, car{"bar", "yellow"}, car{"Baz", "black"}}.Ints())
+}
+
+func TestCars_Float64s(t *testing.T) {
+	assert.Equal(t, Float64s(nil), cars{}.Float64s())
+
+	assert.Equal(t,
+		Float64s{0, 0, 0},
+		cars{car{"a", "green"}, car{"bar", "yellow"}, car{"Baz", "black"}}.Float64s())
+}
diff --git a/pie/float64s_test.go b/pie/float64s_test.go
index 8464faa..6ee153b 100644
--- a/pie/float64s_test.go
+++ b/pie/float64s_test.go
@@ -1071,3 +1071,19 @@ func TestFloat64s_Strings(t *testing.T) {
 		Strings{"92.384", "823.324", "453"},
 		Float64s{92.384, 823.324, 453}.Strings())
 }
+
+func TestFloat64s_Ints(t *testing.T) {
+	assert.Equal(t, Ints(nil), Float64s{}.Ints())
+
+	assert.Equal(t,
+		Ints{92, 823, 453},
+		Float64s{92.384, 823.324, 453}.Ints())
+}
+
+func TestFloat64s_Float64s(t *testing.T) {
+	assert.Equal(t, Float64s(nil), Float64s{}.Float64s())
+
+	assert.Equal(t,
+		Float64s{92.384, 823.324, 453},
+		Float64s{92.384, 823.324, 453}.Float64s())
+}
diff --git a/pie/ints_test.go b/pie/ints_test.go
index c675ddd..cb56af1 100644
--- a/pie/ints_test.go
+++ b/pie/ints_test.go
@@ -1036,3 +1036,19 @@ func TestInts_Strings(t *testing.T) {
 		Strings{"92", "823", "453"},
 		Ints{92, 823, 453}.Strings())
 }
+
+func TestInts_Ints(t *testing.T) {
+	assert.Equal(t, Ints(nil), Ints{}.Ints())
+
+	assert.Equal(t,
+		Ints{92, 823, 453},
+		Ints{92, 823, 453}.Ints())
+}
+
+func TestInts_Float64s(t *testing.T) {
+	assert.Equal(t, Float64s(nil), Ints{}.Float64s())
+
+	assert.Equal(t,
+		Float64s{92, 823, 453},
+		Ints{92, 823, 453}.Float64s())
+}
diff --git a/pie/strings_test.go b/pie/strings_test.go
index 9bd1336..a6bb964 100644
--- a/pie/strings_test.go
+++ b/pie/strings_test.go
@@ -986,3 +986,19 @@ func TestStrings_Strings(t *testing.T) {
 		Strings{"foo", "bar", "BAZ"},
 		Strings{"foo", "bar", "BAZ"}.Strings())
 }
+
+func TestStrings_Ints(t *testing.T) {
+	assert.Equal(t, Ints(nil), Strings{}.Ints())
+
+	assert.Equal(t,
+		Ints{92, 0, 453},
+		Strings{"92.384", "foo", "453"}.Ints())
+}
+
+func TestStrings_Float64s(t *testing.T) {
+	assert.Equal(t, Float64s(nil), Strings{}.Float64s())
+
+	assert.Equal(t,
+		Float64s{92.384, 0, 453},
+		Strings{"92.384", "foo", "453"}.Float64s())
+}