-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d167bcf
commit 0459cd0
Showing
8 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package goterators | ||
|
||
// Average sum of all the source list divided by the total number of source list | ||
func Average[T Number](source []T) float64 { | ||
var sum T | ||
for _, item := range source { | ||
sum += item | ||
} | ||
return float64(sum) / float64(len(source)) | ||
} | ||
|
||
func Mean[T Number](source []T) float64 { | ||
return Average(source) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestAverage(t *testing.T) { | ||
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | ||
expectedValue := 10.5 | ||
|
||
actutalValue := Average(testSource) | ||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
|
||
actutalValue = Mean(testSource) | ||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
} | ||
|
||
func ExampleAverage() { | ||
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | ||
fmt.Println("Average: ", Average(testSource)) | ||
fmt.Println("Mean: ", Mean(testSource)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package goterators | ||
|
||
import "constraints" | ||
|
||
type Number interface { | ||
constraints.Integer | constraints.Float | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package goterators | ||
|
||
// Sum plus all item from source list | ||
func Sum[T Number](source []T) (result T) { | ||
for _, item := range source { | ||
result += item | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package goterators | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestSum(t *testing.T) { | ||
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | ||
expectedValue := 210 | ||
actutalValue := Sum(testSource) | ||
|
||
if actutalValue != expectedValue { | ||
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue) | ||
} | ||
} | ||
|
||
func ExampleSum() { | ||
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | ||
fmt.Println("Sum: ", Sum(testSource)) | ||
} |