Documentation ¶
Overview ¶
Package goterators provides utility functions that support aggregate & transforms list in Go.
Index ¶
- Variables
- func Average[T Number](source []T) float64
- func Count[T constraints.Ordered](source []T, checkedItem T) (result int)
- func CountList[T constraints.Ordered](source []T, checkedItems []T) []int
- func Every[T comparable](source []T, conditionFunc func(item T) bool) bool
- func Exist[T comparable](source []T, checkingItem T) bool
- func Filter[T any](source []T, filteredFunc func(item T) bool) (output []T)
- func Find[T any](source []T, matchedItemFunc func(item T) bool) (t T, index int, err error)
- func Flat[T any](source [][]T) []T
- func FoldLeft[T1 any, T2 any](source []T1, initialValue T2, ...) T2
- func FoldRight[T1 any, T2 any](source []T1, initialValue T2, ...) T2
- func ForEach[T any](source []T, handler func(item T))
- func Group[T any, G comparable](source []T, buildGroup func(item T) G) [][]T
- func Include[T comparable](source, subList []T) bool
- func IncludeSome[T comparable](source, subList []T) bool
- func Map[T1 any, T2 any](source []T1, mappingFunc func(item T1) T2) (output []T2)
- func Max[T constraints.Ordered](source []T) (result T, err error)
- func Mean[T Number](source []T) float64
- func Median[T any](source []T) (T, int, error)
- func MidRange[T Number](source []T) float64
- func Min[T constraints.Ordered](source []T) (result T, err error)
- func Mode[T constraints.Ordered](source []T) (T, int)
- func Range[T Number](source []T) T
- func Reduce[T1 any, T2 any](source []T1, initialValue T2, ...) T2
- func ReduceLeft[T1 any, T2 any](source []T1, initialValue T2, ...) T2
- func ReduceRight[T1 any, T2 any](source []T1, initialValue T2, ...) T2
- func Some[T comparable](source []T, conditionFunc func(item T) bool) bool
- func Sum[T Number](source []T) (result T)
- type Number
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is used in functions that need to find any items in the list but are not found. ErrNotFound = fmt.Errorf("not found") )
Functions ¶
func Average ¶
Average sum of all the source list divided by the total number of source list
Example ¶
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))
Output:
func Count ¶
func Count[T constraints.Ordered](source []T, checkedItem T) (result int)
Count returns number of checking item exists in source list
Example ¶
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4} fmt.Println("Count: ", Count(testSource, 3))
Output:
func CountList ¶
func CountList[T constraints.Ordered](source []T, checkedItems []T) []int
CountList returns sub-list counter of input sub-list that want to count from source list.
Example ¶
testSource := []int{1, 1, 1, 2, 2, 3, 3, 3, 3, 4} fmt.Println("CountList: ", CountList(testSource, []int{1, 1, 2, 3, 5}))
Output:
func Every ¶
func Every[T comparable](source []T, conditionFunc func(item T) bool) bool
Every checks all elements in the list with condition function. If it's yes return true; otherwise, return false.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} everyValid := Every(testSource, func(item int) bool { return item <= 20 }) fmt.Println("Every: ", everyValid)
Output:
func Exist ¶
func Exist[T comparable](source []T, checkingItem T) bool
Exist checks the existence of an element in the list.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} fmt.Println("Exist: ", Exist(testSource, 15))
Output:
func Filter ¶
Filter return items that pass the filter function.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} filteredItems := Filter(testSource, func(item int) bool { return item%2 == 0 }) fmt.Println("Filtered: ", filteredItems)
Output:
func Find ¶
Find returns the first element and its index in the list that meets the functional condition. If no element meet the condition function, return the error "Not Found".
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} firstFoundItem, index, _ := Find(testSource, func(item int) bool { return item%10 == 0 }) fmt.Println("Find: ", firstFoundItem, index)
Output:
func Flat ¶
func Flat[T any](source [][]T) []T
Flat returns a new array with all sub-array elements concatenated with 1 level depth.
Example ¶
testSource := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8, 9, 10, 11}, {12, 13, 14, 15, 16, 17, 18, 19}, {20}, {}} items := Flat(testSource) fmt.Println("Flat: ", items)
Output:
func FoldLeft ¶
func FoldLeft[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2
FoldLeft is same with Reduce()
func FoldRight ¶
func FoldRight[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2
FoldRight is same with Reduce()
func ForEach ¶
func ForEach[T any](source []T, handler func(item T))
ForEach does the same `for` in Go. Just another option to loop through items in a list.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} ForEach(testSource, func(item int) { fmt.Println("ForEach: ", item) })
Output:
func Group ¶
func Group[T any, G comparable](source []T, buildGroup func(item T) G) [][]T
Group groups elements into the nested level. Use a build-group function to define group type.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} groups := Group(testSource, func(item int) int { return item % 2 }) fmt.Println("Group: ", groups)
Output:
func Include ¶
func Include[T comparable](source, subList []T) bool
Include check if source list contains all items from the sub-list item.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} subList := []int{8, 15, 19} result := Include(testSource, subList) fmt.Println("Include: ", result)
Output:
func IncludeSome ¶
func IncludeSome[T comparable](source, subList []T) bool
IncludeSome check if source list contains any items from the sub-list item.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} subList := []int{8, 15, 19} result := IncludeSome(testSource, subList) fmt.Println("IncludeSome: ", result)
Output:
func Map ¶
Map function converts items in the list to the output list.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} mappedItems := Map(testSource, func(item int) string { return fmt.Sprintf("Item-%d", item) }) fmt.Println("Map: ", mappedItems)
Output:
func Max ¶
func Max[T constraints.Ordered](source []T) (result T, err error)
Max find largest value from source list
Example ¶
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} result, _ := Max(testSource) fmt.Println("Max: ", result)
Output:
func Median ¶
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
Example ¶
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)
Output:
func MidRange ¶
MidRange return (max + min) / 2
Example ¶
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} fmt.Println("MidRange: ", MidRange(testSource))
Output:
func Min ¶
func Min[T constraints.Ordered](source []T) (result T, err error)
Min find smallest value from source list
Example ¶
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} result, _ := Min(testSource) fmt.Println("Min: ", result)
Output:
func Mode ¶
func Mode[T constraints.Ordered](source []T) (T, int)
Mode return a value that appears most often in the source list.
Example ¶
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)
Output:
func Range ¶
func Range[T Number](source []T) T
Range return max - min
Example ¶
testSource := []int{20, 17, 9, 21, 18, 3, 11, 5} fmt.Println("Range: ", Range(testSource))
Output:
func Reduce ¶
func Reduce[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2
Reduce runs the reducer function on each element of the array. In order, the reduce function passes in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is the return value of the final reducer on the last element. - list paramerter: source list we want to process. - initial value paramerter: 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 paramerter: the function will run on all elements of the source list.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} result := Reduce(testSource, 0, func(previous int, current int, index int, list []int) int { return previous + current }) fmt.Println("Reduce: ", result)
Output:
func ReduceLeft ¶
func ReduceLeft[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2
ReduceLeft is same with Reduce()
func ReduceRight ¶
func ReduceRight[T1 any, T2 any](source []T1, initialValue T2, reducer func(previousValue T2, currentValue T1, currentIndex int, list []T1) T2) T2
ReduceRight runs the reducer function on each element of the array, from last to the first element. In order, the reduce function passes in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is the return value of the final reducer on the first element.
- list parameter: source list we want to process. - initial value parameter: 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 parameter: the function will run on all elements of the source list.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} result := ReduceRight(testSource, []int{}, func(previous []int, current int, index int, list []int) []int { return append(previous, current) }) fmt.Println("ReduceRight: ", result)
Output:
func Some ¶
func Some[T comparable](source []T, conditionFunc func(item T) bool) bool
Some checks at least one element in the list meet the condition; return true, or return false if all elements don't meet the condition.
Example ¶
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} result := Some(testSource, func(item int) bool { return item%2 == 0 }) fmt.Println("Some: ", result)
Output:
Types ¶
type Number ¶
type Number interface { constraints.Integer | constraints.Float }