Repository: elliotchance/pie
Pull request #83 (2019-05-03 01:57:08)

Adding Abs (#67)
Let me know if anything needs to be changed or I did something wrong. First time doing anything with Go.

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


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

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

```diff
@@          Coverage Diff          @@
##           master    #83   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           7      7           
  Lines         690    698    +8     
=====================================
+ Hits          690    698    +8
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/elliotchance/pie/pull/83?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/83?src=pr&el=footer). Last update [70bf87c...34f86ef](https://codecov.io/gh/elliotchance/pie/pull/83?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 4cfaed2..9ad1ffe 100644
--- a/README.md
+++ b/README.md
@@ -124,6 +124,7 @@ This will only generate `myInts.Average`, `myInts.Sum` and `myStrings.Select`.
 
 | Function     | String | Number | Struct| Maps | Big-O    | Description |
 | ------------ | :----: | :----: | :----:| :--: | :------: | ----------- |
+| `Abs`        |        | ✓      |       |      | n        | Abs will return the absolute value of all values in the slice.
 | `All`        | ✓      | ✓      | ✓     |      | n        | All will return true if all callbacks return true. If the list is empty then true is always returned. |
 | `Any`        | ✓      | ✓      | ✓     |      | n        | Any will return true if any callbacks return true. If the list is empty then false is always returned. |
 | `Append`     | ✓      | ✓      | ✓     |      | n        | A new slice with the elements appended to the end. |
diff --git a/functions/abs.go b/functions/abs.go
new file mode 100644
index 0000000..ff8c63c
--- /dev/null
+++ b/functions/abs.go
@@ -0,0 +1,14 @@
+package functions
+
+import (
+	"math"
+)
+
+// Abs is a function which returns the absolute value of all the
+// elements in the slice.
+func (ss SliceType) Abs() SliceType {
+	for i, val := range ss {
+		ss[i] = ElementType(math.Abs(float64(val)))
+	}
+	return ss
+}
diff --git a/functions/main.go b/functions/main.go
index d0348c0..bc65ad3 100644
--- a/functions/main.go
+++ b/functions/main.go
@@ -18,6 +18,7 @@ var Functions = []struct {
 	File string
 	For  int
 }{
+	{"Abs", "abs.go", ForNumbers},
 	{"All", "all.go", ForAll},
 	{"Any", "any.go", ForAll},
 	{"Append", "append.go", ForAll},
diff --git a/pie/float64s_pie.go b/pie/float64s_pie.go
index 0107a9b..5874feb 100755
--- a/pie/float64s_pie.go
+++ b/pie/float64s_pie.go
@@ -3,10 +3,20 @@ package pie
 import (
 	"encoding/json"
 	"github.com/elliotchance/pie/pie/util"
+	"math"
 	"math/rand"
 	"sort"
 )
 
+// Abs is a function which returns the absolute value of all the
+// elements in the slice.
+func (ss Float64s) Abs() Float64s {
+	for i, val := range ss {
+		ss[i] = float64(math.Abs(float64(val)))
+	}
+	return ss
+}
+
 // All will return true if all callbacks return true. It follows the same logic
 // as the all() function in Python.
 //
diff --git a/pie/ints_pie.go b/pie/ints_pie.go
index 5136695..e00b361 100755
--- a/pie/ints_pie.go
+++ b/pie/ints_pie.go
@@ -3,10 +3,20 @@ package pie
 import (
 	"encoding/json"
 	"github.com/elliotchance/pie/pie/util"
+	"math"
 	"math/rand"
 	"sort"
 )
 
+// Abs is a function which returns the absolute value of all the
+// elements in the slice.
+func (ss Ints) Abs() Ints {
+	for i, val := range ss {
+		ss[i] = int(math.Abs(float64(val)))
+	}
+	return ss
+}
+
 // All will return true if all callbacks return true. It follows the same logic
 // as the all() function in Python.
 //
diff --git a/template.go b/template.go
index 97d728d..d5021cf 100644
--- a/template.go
+++ b/template.go
@@ -2,6 +2,21 @@
 package main
 
 var pieTemplates = map[string]string{
+	"Abs": `package functions
+
+import (
+	"math"
+)
+
+// Abs is a function which returns the absolute value of all the
+// elements in the slice.
+func (ss SliceType) Abs() SliceType {
+	for i, val := range ss {
+		ss[i] = ElementType(math.Abs(float64(val)))
+	}
+	return ss
+}
+`,
 	"All": `package functions
 
 // All will return true if all callbacks return true. It follows the same logic


Test patch:
diff --git a/pie/float64s_test.go b/pie/float64s_test.go
index 4da645e..42bd299 100644
--- a/pie/float64s_test.go
+++ b/pie/float64s_test.go
@@ -685,3 +685,29 @@ func TestFloat64s_Each(t *testing.T) {
 	})
 	assert.Equal(t, []float64{435.34, 8923.1}, values)
 }
+
+var float64sAbsTests = []struct {
+	ss  Float64s
+	abs Float64s
+}{
+	{
+		Float64s{1, 2, 3, 4, 5},
+		Float64s{1, 2, 3, 4, 5},
+	},
+	{
+		Float64s{636, -5828, 444, -29281, 0},
+		Float64s{636, 5828, 444, 29281, 0},
+	},
+	{
+		Float64s{-584.2727, -47474.2112, 96843, -0.000004, 13},
+		Float64s{584.2727, 47474.2112, 96843, 0.000004, 13},
+	},
+}
+
+func TestFloat64s_Abs(t *testing.T) {
+	for _, test := range float64sAbsTests {
+		t.Run("", func(t *testing.T) {
+			assert.Equal(t, test.abs, test.ss.Abs())
+		})
+	}
+}
diff --git a/pie/ints_test.go b/pie/ints_test.go
index b93c6b4..e0d95e2 100644
--- a/pie/ints_test.go
+++ b/pie/ints_test.go
@@ -671,3 +671,9 @@ func TestInts_Each(t *testing.T) {
 	})
 	assert.Equal(t, []int{435, 8923}, values)
 }
+
+func TestMyInts_Abs(t *testing.T) {
+	assert.Equal(t, Ints{1, 5, 7}, Ints{-1, 5, -7}.Abs())
+	assert.Equal(t, Ints{689845, 688969, 220373, 89437, 308836}, Ints{-689845, -688969, -220373, -89437, 308836}.Abs())
+	assert.Equal(t, Ints{1, 2}, Ints{1, 2}.Abs())
+}