Skip to content

miyamo2/xtract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xtract

Go Reference GitHub go.mod Go version GitHub release (latest by date) codecov Go Report Card GitHub License

Extract from collection and build iterators.

Quick Start

Install

go get github.com/miyamo2/xtract

Setup GOEXPERIMENT

Important

If your Go project is Go 1.23 or higher, this section is not necessary.

Also, if Go1.21 or lower, you will need to update to Go1.22.

go env -w GOEXPERIMENT=rangefunc

Usage

With SliceExtractor.ByValue

s := []string{"gopher", "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: gopher
//iterator

With SliceExtractor.ByKey

s := []string{"gopher", "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: gopher
//iterator

With SliceExtractor.ByKeyAndValue

s := []string{"gopher", "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

With MapExtractor.ByValue

m := map[string]string{"language": "gopher", "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: gopher

With MapExtractor.ByKey

m := map[string]string{"language": "gopher", "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

With MapExtractor.ByKeyAndValue

m := map[string]string{"language": "gopher", "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: gopher

Contributing

Feel free to open a PR or an Issue.

License

xtract released under the MIT License

About

Extract collection and build iterator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages