Extract from collection and build iterator.
go get github.com/miyamo2/xtract
Important
If your Go project is Go 1.23 or higher, this section is not necessary.
Also, if Go 1.21 or lower, you will need to update to Go 1.22.
go env -w GOEXPERIMENT=rangefunc
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101}
xt := xtract.FromSlice(s)
even := xt.ByValue(func(i int) bool { return i%2 == 0 })
odd := xt.ByValue(func(i int) bool { return i%2 != 0 })
fmt.Println("---even---")
for v := range even.Values() {
fmt.Println(v)
}
fmt.Println("---odd---")
for v := range odd.Values() {
fmt.Println(v)
}
evenAndTwoDigits := even.ByValue(func(i int) bool { return i > 9 && i < 100 })
fmt.Println("---even and two digits---")
for v := range evenAndTwoDigits.Values() {
fmt.Println(v)
}
oddAndTwoDigits := odd.ByValue(func(i int) bool { return i > 9 && i < 100 })
fmt.Println("---odd and two digits---")
for v := range oddAndTwoDigits.Values() {
fmt.Println(v)
}
// Output: ---even---
//0
//2
//4
//6
//8
//10
//100
//---odd---
//1
//3
//5
//7
//9
//11
//101
//---even and two digits---
//10
//---odd and two digits---
//11
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByValue(func(v string) bool { return len(v) < 9 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
//iterator
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByKey(func(i int) bool { return i > 0 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
//iterator
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByKeyAndValue(func(i int, v string) bool { return i > 1 && len(v) > 6 })
for v := range xt.Values()
fmt.Println(v)
}
// Output: range over func
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).Limit(1)
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).Offset(1)
for v := range xt.Values() {
fmt.Println(v)
}
// Output: iterator
//range over func
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByValue(func(v string) bool { return len(v) < 8 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKey(func(k string) bool { return strings.Contains(k, " ") })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: iterator
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKeyAndValue(func(k, v string) bool { return strings.Contains(k, "e") && len(v) < 8 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).Limit(1)
values := make([]string, 0, 1)
for v := range xt.Values() {
values = append(values, v)
}
fmt.Println(len(values))
// Output: 1
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).Offset(1)
values := make([]string, 0, 1)
for v := range xt.Values() {
values = append(values, v)
}
fmt.Println(len(values))
// Output: 2
Feel free to open a PR or an Issue.
xtract released under the MIT License