Skip to content

Commit

Permalink
Add Sum() and Average()
Browse files Browse the repository at this point in the history
  • Loading branch information
ledongthuc committed Jan 5, 2022
1 parent d167bcf commit 0459cd0
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
- [exist](#exist)
- [include](#include)
- [include some](#include-some)
- [sum](#sum)
- [average](#average)

- Support generic functions that fit with all your types.
- The API and functions are inspired by Rust and Javascript.

# Requirement
Expand Down Expand Up @@ -93,6 +96,7 @@ matchedStruct, index, err := Find(list, func(item MyStruct) bool {
- list: source list we want to process.
- initial value: the previous value that's used in the reducer call of the first element. At this time, previous = initial value, current = first element of the list.
- reducer function: the function will run on all elements of the source list.
- Can use Reduce() or ReduceLeft() or FoldLeft()

```go
// Sum
Expand All @@ -115,6 +119,7 @@ items := Reduce(testSource, []float64{}, func(previous []float64, current int, i
- list: source list we want to process.
- initial value: the previous value that's used in the reducer call of the last element. At this time, previous = initial value, current = last element of list.
- reducer function: the function will run on all elements of the source list.
- Can use ReduceRight() or FoldRight()

```go
// Reverse
Expand Down Expand Up @@ -245,6 +250,30 @@ result := IncludeSome(list, subList)
fmt.Println("IncludeSome: ", result)
```

## Sum

![goterators-Sum](https://user-images.githubusercontent.com/1828895/148277403-70da16a7-5314-42d0-a9bf-c59bc3f0cba5.png)

- Sum plus all item from source list

```go
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))
```

## Average

![goterators-Average](https://user-images.githubusercontent.com/1828895/148277508-66a08f9e-22ec-4e04-8808-69bee8649871.png)

- Average sum of all the source list divided by the total number of source list
- We can use Average() or Mean()

```go
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))
```

# License

MIT
Expand Down
14 changes: 14 additions & 0 deletions average.go
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)
}
27 changes: 27 additions & 0 deletions average_test.go
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))
}
7 changes: 7 additions & 0 deletions constraints.go
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
}
10 changes: 10 additions & 0 deletions reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ func Reduce[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousV
}
return output
}

// ReduceLeft is same with Reduce()
func ReduceLeft[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2 {
return Reduce(source, initialValue, reducer)
}

// FoldLeft is same with Reduce()
func FoldLeft[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2 {
return Reduce(source, initialValue, reducer)
}
5 changes: 5 additions & 0 deletions reduce_right.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ func ReduceRight[T1 any, T2 any](source []T1, initialValue T2, reducer func(prev
}
return output
}

// FoldRight is same with Reduce()
func FoldRight[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2 {
return Reduce(source, initialValue, reducer)
}
9 changes: 9 additions & 0 deletions sum.go
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
}
21 changes: 21 additions & 0 deletions sum_test.go
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))
}

0 comments on commit 0459cd0

Please sign in to comment.