Popularity
2.5
Growing
Activity
6.9
-
34
1
4

Description

A utility library that supports aggregate & transforms functions Go with generic. Library support Filter(), Map(), Reduce(), ReduceRight(), Every(), Some(), Group(), Flat(), Exist(), Include(), IncludeSome(), Count(), CountList(), Sum(), Average(), Mean(), Median(), Mode(), Range(), MidRange()

Programming language: Go
License: MIT License
Tags: Functional     Reduce     Utility    

Goterators alternatives and similar packages

Based on the "Functional" category.
Alternatively, view goterators alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of Goterators or a related project?

Add another 'Functional' Package

README

Goterators

Built with WeBuild Go Reference

goterators-Thumbnail

Requirement

  • Go 1.18

Installation

This assumes you already have a working Go environment.

Use Go get to pull goterators for using

go get github.com/ledongthuc/goterators

Usage

Import the package goterators into your project before using.

import "github.com/ledongthuc/goterators"

Functions

For-each

goterators-ForEach

  • For-each function does the same for in Go. Just another option to loop through items in a list.
ForEach(list1, func(item int) {
  // handle each item in the list
})

ForEach(list2, func(item string) {
  // handle each item in the list
})

ForEach(list3, func(item MyStruct) {
  // handle each item in the list
})

Find

goterators-Find

  • Find function 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".
matchedInt, index, err := Find(list, func(item int) bool {
  return item == 1
})

matchedString, index, err := Find(list, func(item string) bool {
  return item == "searching text"
})

matchedStruct, index, err := Find(list, func(item MyStruct) bool {
  return item == searchingStruct
})

Reduce

goterators-Reduce

  • Similar to Fold Left, Reduce function 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.
  • Reduce function has 3 parameters:
    • 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.
// Sum
total := Reduce(list, 0, func(previous int, current int, index int, list []int) int {
    return previous + current
})

// Mapping ints to floats
items := Reduce(testSource, []float64{}, func(previous []float64, current int, index int, list []int) []float64 {
    return append(previous, float64(current))
})

Reduce right

goterators-Reduce right

  • Similar to Fold Right, Reduce right function run 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.
  • Reduce function has 3 parameters:
    • 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.
// Reverse
reversedList := Reduce(list, []string{}, func(previous []string, current string, index int, list []string) []string {
  return append(list, current)
})

Filter

goterators-Filter

  • Filter function return items that pass the filter function.
filteredItems, err := Filter(list, func(item int) bool {
  return item % 2 == 0
})

filteredItems, err := Filter(list, func(item string) bool {
  return item.Contains("ValidWord")
})

filteredItems, err := Filter(list, func(item MyStruct) bool {
  return item.Valid()
})

Map

goterators-Map

  • Map function converts items in the list to the output list.
mappedItems := Map(testSource, func(item int64) float64 {
  return float64(item)
})

prices := Map(testSource, func(item Order) Price {
  return item.GetPrice()
})

Every

goterators-Every

  • Every function checks all elements in the list with condition function. If it's yes return true; otherwise, return false.
valid := Every(list, func(item int) bool { item % 2 == 0 }) 

valid := Every(list, func(item string) bool { len(item) < 20 }) 

Some

goterators-Some

  • Some function check at least one element in the list meet the condition; return true, or return false if all elements don't meet the condition.
valid := Some(list, func(item int) bool { item % 2 == 0 }) 

valid := Some(list, func(item string) bool { len(item) < 20 }) 

Group

goterators-Group

  • Group groups elements into the nested level. Use a build-group function to define group type.
groups := Group(list, func(item Product) groupKey {
   return item.ComposeGroupKey()
}) // Group contains [ [ Product1, Product2, Product3 ], [ Product4, Product5 ] ]

Flat

goterators-Flat

  • Flat returns a new array with all sub-array elements concatenated with 1 level depth.
output := Flat([][]int{{1,2}, {3}}) // output = {1,2,3}

Exist

goterators-Exist

  • Exist function checks the existence of an element in the list.
matchedInt, err := Exist(list, 1)

matchedString, err := Exist(list, "searching string")

matchedStruct, err := Exist(list, SearchingStruct)

Include

goterators-Include

  • Include check if source list contains all items from the sub-list item.
list := []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(list, subList)
fmt.Println("Include: ", result)

Include some

goterators-IncludeSome

  • IncludeSome check if source list contains any items from the sub-list item.
list := []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(list, subList)
fmt.Println("IncludeSome: ", result)

License

MIT

Contribution

All your contributions to project and make it better, they are welcome. Feel free to reach me https://thuc.space or create an issue to start it.

Thanks! 🙌

Stargazers repo roster for @ledongthuc/goterators


*Note that all licence references and agreements mentioned in the Goterators README section above are relevant to that project's source code only.