Skip to content

Commit

Permalink
Add Count(), CountList(), Mode(), Max(), Min(), Median(), Range(),
Browse files Browse the repository at this point in the history
MidRange()
  • Loading branch information
ledongthuc committed Jan 6, 2022
1 parent 0459cd0 commit 7e0ceae
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 6 deletions.
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
- [exist](#exist)
- [include](#include)
- [include some](#include-some)
- [count](#count)
- [count list](#count-list)
- [mode](#mode)
- [sum](#sum)
- [average](#average)
- [max](#max)
- [min](#min)
- [range](#range)
- [mid range](#mid-range)

- Support generic functions that fit with all your types.
- The API and functions are inspired by Rust and Javascript.
Expand Down Expand Up @@ -250,6 +257,40 @@ result := IncludeSome(list, subList)
fmt.Println("IncludeSome: ", result)
```

## Count

![goterators-Count](https://user-images.githubusercontent.com/1828895/148364553-384b5dfd-404e-4c58-98d4-e4ea5a8b108c.png)

- Count returns number of checking item exists in source list

```go
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4}
fmt.Println("Count: ", Count(testSource, 3))
```

## Count list

![goterators-CountList](https://user-images.githubusercontent.com/1828895/148364711-68ad21bf-24f7-4db3-bd00-71153130252d.png)

- CountList returns sub-list counter of input sub-list that want to count from source list.

```go
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4}
fmt.Println("CountList: ", CountList(testSource, []int{1, 1, 2, 3, 5}))
```

## Mode

![goterators-Mode](https://user-images.githubusercontent.com/1828895/148366681-67050e42-9970-425f-a960-c2d76914a501.png)

- Mode return a value that appears most often in the 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}
mostOftenValue, counter := Mode(testSource)
fmt.Println("Mode: ", mostOftenValue, counter)
```

## Sum

![goterators-Sum](https://user-images.githubusercontent.com/1828895/148277403-70da16a7-5314-42d0-a9bf-c59bc3f0cba5.png)
Expand All @@ -274,6 +315,64 @@ fmt.Println("Average: ", Average(testSource))
fmt.Println("Mean: ", Mean(testSource))
```

## Max

![goterators-Max](https://user-images.githubusercontent.com/1828895/148365002-57f7c80e-4901-4a97-abe0-9fe38f9e9382.png)

- Max find largest value from source list

```go
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
result, err := Max(testSource)
fmt.Println("Max: ", result)
```

## Min

![goterators-Min](https://user-images.githubusercontent.com/1828895/148365126-883eacc6-b062-444d-afa5-d05606d79a66.png)

- Min find smallest value from source list

```go
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
result, _ := Min(testSource)
fmt.Println("Min: ", result)
```

## Median

![goterators-Median](https://user-images.githubusercontent.com/1828895/148365514-4adb7c24-8458-4181-b401-fbebfe5b64de.png)

- Median return a value in the middle of an ordered source list. If number of item in source is even, return right item. Make sure source list are sorted

```go
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
median, index, _ := Median(testSource)
fmt.Println("Median: ", median, ", with index: ", index)
```

## Range

![goterators-Range](https://user-images.githubusercontent.com/1828895/148365855-fedf833e-7c40-42d7-a331-3d1f6ce7cdde.png)

- Range return max - min

```go
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
fmt.Println("Range: ", Range(testSource))
```

## Mid range

![goterators-MidRange](https://user-images.githubusercontent.com/1828895/148366240-a15b9c96-8edf-418c-a284-5d54fd2c91a6.png)

- MidRange return (max + min) / 2

```go
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
fmt.Println("Range: ", Range(testSource))
```

# License

MIT
Expand Down
12 changes: 6 additions & 6 deletions average_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ 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)
actualValue := Average(testSource)
if actualValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actualValue)
}

actutalValue = Mean(testSource)
if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
actualValue = Mean(testSource)
if actualValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actualValue)
}
}

Expand Down
13 changes: 13 additions & 0 deletions count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package goterators

import "constraints"

// Count returns number of checking item exists in source list
func Count[T constraints.Ordered](source []T, checkedItem T) (result int) {
for _, item := range source {
if item == checkedItem {
result++
}
}
return result
}
12 changes: 12 additions & 0 deletions count_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package goterators

import "constraints"

// CountList returns sub-list counter of input sub-list that want to count from source list.
func CountList[T constraints.Ordered](source []T, checkedItems []T) []int {
result := make([]int, len(checkedItems))
for index, item := range checkedItems {
result[index] = Count(source, item)
}
return result
}
27 changes: 27 additions & 0 deletions count_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package goterators

import (
"fmt"
"testing"
)

func TestCountList(t *testing.T) {
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4}

countingItems := []int{1, 1, 2, 3, 5}
expectedValue := []int{3, 3, 2, 4, 0}
actualValue := CountList(testSource, countingItems)
if len(actualValue) != len(expectedValue) {
t.Fatalf("Expected length = %v , got length = %v", len(expectedValue), len(actualValue))
}
for index := range expectedValue {
if expectedValue[index] != actualValue[index] {
t.Errorf("Expected = %v , got = %v", expectedValue[index], actualValue[index])
}
}
}

func ExampleCountList() {
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4}
fmt.Println("CountList: ", CountList(testSource, []int{1, 1, 2, 3, 5}))
}
29 changes: 29 additions & 0 deletions count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goterators

import (
"fmt"
"testing"
)

func TestCount(t *testing.T) {
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4}

findingItem := 3
expectedValue := 4
actutalValue := Count(testSource, findingItem)
if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}

findingItem = 5
expectedValue = 0
actutalValue = Count(testSource, findingItem)
if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}
}

func ExampleCount() {
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4}
fmt.Println("Count: ", Count(testSource, 3))
}
17 changes: 17 additions & 0 deletions max.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package goterators

import "constraints"

// Max find largest value from source list
func Max[T constraints.Ordered](source []T) (result T, err error) {
if len(source) == 0 {
return result, ErrNotFound
}
result = source[0]
for _, item := range source {
if item > result {
result = item
}
}
return result, nil
}
22 changes: 22 additions & 0 deletions max_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package goterators

import (
"fmt"
"testing"
)

func TestMax(t *testing.T) {
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
expectedValue := 21
actutalValue, _ := Max(testSource)

if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}
}

func ExampleMax() {
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
result, _ := Max(testSource)
fmt.Println("Max: ", result)
}
14 changes: 14 additions & 0 deletions median.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package goterators

// Median return a value in the middle of an ordered source list. If number of item in source is even, return right item. Make sure source list are sorted
func Median[T any](source []T) (T, int, error) {
if len(source) == 0 {
var result T
return result, 0, ErrNotFound
}
if len(source) == 1 {
return source[0], 0, nil
}
midNumber := len(source) / 2
return source[midNumber], midNumber, nil
}
36 changes: 36 additions & 0 deletions median_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package goterators

import (
"fmt"
"testing"
)

func TestMedian(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 := 11
expectedIndex := 10
actutalValue, actualIndex, _ := Median(testSource)
if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}
if actualIndex != expectedIndex {
t.Errorf("Expected = %v , got = %v", expectedIndex, actualIndex)
}

testSource = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
expectedValue = 10
expectedIndex = 9
actutalValue, actualIndex, _ = Median(testSource)
if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}
if actualIndex != expectedIndex {
t.Errorf("Expected = %v , got = %v", expectedIndex, actualIndex)
}
}

func ExampleMedian() {
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
median, index, _ := Median(testSource)
fmt.Println("Median: ", median, ", with index: ", index)
}
11 changes: 11 additions & 0 deletions mid_range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package goterators

// MidRange return (max + min) / 2
func MidRange[T Number](source []T) float64 {
if len(source) <= 1 {
return 0.0
}
max, _ := Max(source)
min, _ := Min(source)
return float64(max+min) / 2.0
}
21 changes: 21 additions & 0 deletions mid_range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package goterators

import (
"fmt"
"testing"
)

func TestMidRange(t *testing.T) {
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
expectedValue := 12.0
actutalValue := MidRange(testSource)

if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}
}

func ExampleMidRange() {
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
fmt.Println("MidRange: ", MidRange(testSource))
}
17 changes: 17 additions & 0 deletions min.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package goterators

import "constraints"

// Min find smallest value from source list
func Min[T constraints.Ordered](source []T) (result T, err error) {
if len(source) == 0 {
return result, ErrNotFound
}
result = source[0]
for _, item := range source {
if item < result {
result = item
}
}
return result, nil
}
22 changes: 22 additions & 0 deletions min_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package goterators

import (
"fmt"
"testing"
)

func TestMin(t *testing.T) {
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
expectedValue := 3
actutalValue, _ := Min(testSource)

if actutalValue != expectedValue {
t.Errorf("Expected = %v , got = %v", expectedValue, actutalValue)
}
}

func ExampleMin() {
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5}
result, _ := Min(testSource)
fmt.Println("Min: ", result)
}
Loading

0 comments on commit 7e0ceae

Please sign in to comment.