Skip to content

Commit

Permalink
Merge pull request #17 from ledongthuc/feature/add_more_funcs
Browse files Browse the repository at this point in the history
Update readme
  • Loading branch information
ledongthuc authored Mar 15, 2022
2 parents 41aee43 + 45a5c48 commit db1d343
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@

![goterators-Thumbnail](https://user-images.githubusercontent.com/1828895/147876484-5bc7cfd0-5f14-4889-a3f0-64cb307b7765.png)

- Goterators is util library that support aggregate & transforms functions list in Go, including:
- Goterators is util library that support aggregate & transforms functions list in Go.
- Iterate:
- [for-each](#for-each)
- [find](#find)
- [reduce](#reduce)
- [reduce right](#reduce-right)
- [filter](#filter)
- [map](#map)
- [flat](#flat)

- Condition:
- [every](#every)
- [some](#some)
- [group](#group)
- [flat](#flat)
- [exist](#exist)
- [include](#include)
- [include some](#include-some)

- Aggregate:
- [count](#count)
- [count list](#count-list)
- [mode](#mode)
Expand Down Expand Up @@ -62,15 +67,15 @@ import "github.com/ledongthuc/goterators"
- For-each function does the same `for` in Go. Just another option to loop through items in a list.

```go
ForEach(list1, func(item int) {
goterators.ForEach(list1, func(item int) {
// handle each item in the list
})

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

ForEach(list3, func(item MyStruct) {
goterators.ForEach(list3, func(item MyStruct) {
// handle each item in the list
})
```
Expand All @@ -82,15 +87,15 @@ ForEach(list3, func(item MyStruct) {
- 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".

```go
matchedInt, index, err := Find(list, func(item int) bool {
matchedInt, index, err := goterators.Find(list, func(item int) bool {
return item == 1
})

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

matchedStruct, index, err := Find(list, func(item MyStruct) bool {
matchedStruct, index, err := goterators.Find(list, func(item MyStruct) bool {
return item == searchingStruct
})
```
Expand All @@ -108,12 +113,12 @@ matchedStruct, index, err := Find(list, func(item MyStruct) bool {

```go
// Sum
total := Reduce(list, 0, func(previous int, current int, index int, list []int) int {
total := goterators.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 {
items := goterators.Reduce(testSource, []float64{}, func(previous []float64, current int, index int, list []int) []float64 {
return append(previous, float64(current))
})
```
Expand All @@ -131,7 +136,7 @@ items := Reduce(testSource, []float64{}, func(previous []float64, current int, i

```go
// Reverse
reversedList := Reduce(list, []string{}, func(previous []string, current string, index int, list []string) []string {
reversedList := goterators.Reduce(list, []string{}, func(previous []string, current string, index int, list []string) []string {
return append(list, current)
})
```
Expand All @@ -143,15 +148,15 @@ reversedList := Reduce(list, []string{}, func(previous []string, current string,
- Filter function return items that pass the filter function.

```go
filteredItems, err := Filter(list, func(item int) bool {
filteredItems, err := goterators.Filter(list, func(item int) bool {
return item % 2 == 0
})

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

filteredItems, err := Filter(list, func(item MyStruct) bool {
filteredItems, err := goterators.Filter(list, func(item MyStruct) bool {
return item.Valid()
})
```
Expand All @@ -163,11 +168,11 @@ filteredItems, err := Filter(list, func(item MyStruct) bool {
- Map function converts items in the list to the output list.

```go
mappedItems := Map(testSource, func(item int64) float64 {
mappedItems := goterators.Map(testSource, func(item int64) float64 {
return float64(item)
})

prices := Map(testSource, func(item Order) Price {
prices := goterators.Map(testSource, func(item Order) Price {
return item.GetPrice()
})
```
Expand All @@ -179,9 +184,9 @@ prices := Map(testSource, func(item Order) Price {
- Every function checks all elements in the list with condition function. If it's yes return true; otherwise, return false.

```go
valid := Every(list, func(item int) bool { item % 2 == 0 })
valid := goterators.Every(list, func(item int) bool { item % 2 == 0 })

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

## Some
Expand All @@ -191,9 +196,9 @@ valid := Every(list, func(item string) bool { len(item) < 20 })
- 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.

```go
valid := Some(list, func(item int) bool { item % 2 == 0 })
valid := goterators.Some(list, func(item int) bool { item % 2 == 0 })

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

## Group
Expand All @@ -203,7 +208,7 @@ valid := Some(list, func(item string) bool { len(item) < 20 })
- Group groups elements into the nested level. Use a build-group function to define group type.

```
groups := Group(list, func(item Product) groupKey {
groups := goterators.Group(list, func(item Product) groupKey {
return item.ComposeGroupKey()
}) // Group contains [ [ Product1, Product2, Product3 ], [ Product4, Product5 ] ]
```
Expand All @@ -215,7 +220,7 @@ groups := Group(list, func(item Product) groupKey {
- Flat returns a new array with all sub-array elements concatenated with 1 level depth.

```go
output := Flat([][]int{{1,2}, {3}}) // output = {1,2,3}
output := goterators.Flat([][]int{{1,2}, {3}}) // output = {1,2,3}
```

## Exist
Expand All @@ -225,11 +230,11 @@ output := Flat([][]int{{1,2}, {3}}) // output = {1,2,3}
- Exist function checks the existence of an element in the list.

```go
matchedInt, err := Exist(list, 1)
matchedInt, err := goterators.Exist(list, 1)

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

matchedStruct, err := Exist(list, SearchingStruct)
matchedStruct, err := goterators.Exist(list, SearchingStruct)
```

## Include
Expand All @@ -241,7 +246,7 @@ matchedStruct, err := Exist(list, SearchingStruct)
```go
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)
result := goterators.Include(list, subList)
fmt.Println("Include: ", result)
```

Expand All @@ -254,7 +259,7 @@ fmt.Println("Include: ", result)
```go
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)
result := goterators.IncludeSome(list, subList)
fmt.Println("IncludeSome: ", result)
```

Expand All @@ -266,7 +271,7 @@ fmt.Println("IncludeSome: ", result)

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

## Count list
Expand All @@ -277,7 +282,7 @@ fmt.Println("Count: ", Count(testSource, 3))

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

## Mode
Expand All @@ -288,7 +293,7 @@ fmt.Println("CountList: ", CountList(testSource, []int{1, 1, 2, 3, 5}))

```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)
mostOftenValue, counter := goterators.Mode(testSource)
fmt.Println("Mode: ", mostOftenValue, counter)
```

Expand All @@ -300,7 +305,7 @@ fmt.Println("Mode: ", mostOftenValue, counter)

```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))
fmt.Println("Sum: ", goterators.Sum(testSource))
```

## Average
Expand All @@ -312,8 +317,8 @@ fmt.Println("Sum: ", Sum(testSource))

```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))
fmt.Println("Average: ", goterators.Average(testSource))
fmt.Println("Mean: ", goterators.Mean(testSource))
```

## Max
Expand All @@ -324,7 +329,7 @@ fmt.Println("Mean: ", Mean(testSource))

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

Expand All @@ -336,7 +341,7 @@ fmt.Println("Max: ", result)

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

Expand All @@ -348,7 +353,7 @@ fmt.Println("Min: ", result)

```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)
median, index, _ := goterators.Median(testSource)
fmt.Println("Median: ", median, ", with index: ", index)
```

Expand All @@ -360,7 +365,7 @@ fmt.Println("Median: ", median, ", with index: ", index)

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

## Mid range
Expand All @@ -371,7 +376,7 @@ fmt.Println("Range: ", Range(testSource))

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

# License
Expand Down

0 comments on commit db1d343

Please sign in to comment.