Skip to content

miyamo2/xtract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 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 iterator.

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 Go 1.21 or lower, you will need to update to Go 1.22.

go env -w GOEXPERIMENT=rangefunc

Usage

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

With SliceExtractor.ByValue

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

With SliceExtractor.ByKey

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

With SliceExtractor.ByKeyAndValue

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

With SliceExtractor.Limit

s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).Limit(1)
for v := range xt.Values() {
    fmt.Println(v)
}
// Output: go

With SliceExtractor.Offset

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

With MapExtractor.ByValue

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

With MapExtractor.ByKey

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

With MapExtractor.ByKeyAndValue

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

With MapExtractor.Limit

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

With MapExtractor.Offset

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

Contributing

Feel free to open a PR or an Issue.

License

xtract released under the MIT License