xtract

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2024 License: MIT Imports: 1 Imported by: 0

README

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"}
for v := range xtract.FromSlice(s).ByValue(func(v string) bool { return len(v) < 9 }) {
    fmt.Println(v)
}
// Output: gopher
//iterator
With SliceExtractor.ByKey
s := []string{"gopher", "iterator", "range over func"}
for v := range xtract.FromSlice(s).ByKey(func(i int) bool { return i < 2 }) {
    fmt.Println(v)
}
// Output: gopher
//iterator
With SliceExtractor.ByKeyAndValue
s := []string{"gopher", "iterator", "range over func"}
for v := range xtract.FromSlice(s).ByKeyAndValue(func(i int, v string) bool { return i > 1 && len(v) > 6 }) {
    fmt.Println(v)
}
// Output: range over func
With MapExtractor.ByValue
m := map[string]string{"language": "gopher", "design pattern": "iterator", "implementation": "range over func"}
for v := range xtract.FromMap(m).ByValue(func(v string) bool { return len(v) < 8 }) {
    fmt.Println(v)
}
// Output: gopher
With MapExtractor.ByKey
m := map[string]string{"language": "gopher", "design pattern": "iterator", "implementation": "range over func"}
for v := range xtract.FromMap(m).ByKey(func(k string) bool { return strings.Contains(k, " ") }) {
    fmt.Println(v)
}
// Output: iterator
With MapExtractor.ByKeyAndValue
m := map[string]string{"language": "gopher", "design pattern": "iterator", "implementation": "range over func"}
for v := range xtract.FromMap(m).ByKeyAndValue(func(k, v string) bool { return strings.Contains(k, "e") && len(v) < 8 }) {
    fmt.Println(v)
}
// Output: gopher

Contributing

Feel free to open a PR or an Issue.

License

xtract released under the MIT License

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Extractor

type Extractor[K comparable, V any] interface {
	// ByValue filters the values of the collection by their values.
	ByValue(condition func(V) bool) Extractor[K, V]
	// ByKey filters the values of the collection by their keys.
	ByKey(condition func(K) bool) Extractor[K, V]
	// ByKeyAndValue filters the values of the collection by their keys and values.
	ByKeyAndValue(condition func(K, V) bool) Extractor[K, V]
	// Values returns a sequence of values.
	Values() iter.Seq[V]
}

Extractor provides features to extract values from a collection.

func FromMap

func FromMap[K comparable, V any](m map[K]V) Extractor[K, V]

FromMap returns Extractor for a map.

func FromSlice

func FromSlice[V any](s []V) Extractor[int, V]

FromSlice returns Extractor for a slice.

type MapExtractor

type MapExtractor[K comparable, V any] struct {
	// contains filtered or unexported fields
}

MapExtractor is implementation of Extractor for map.

func (MapExtractor[K, V]) ByKey

func (x MapExtractor[K, V]) ByKey(condition func(K) bool) Extractor[K, V]

ByKey See: Extractor.ByKey

Example
package main

import (
	"fmt"
	"github.com/miyamo2/xtract"
	"strings"
)

func main() {
	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

func (MapExtractor[K, V]) ByKeyAndValue

func (x MapExtractor[K, V]) ByKeyAndValue(condition func(K, V) bool) Extractor[K, V]

ByKeyAndValue See: Extractor.ByKeyAndValue

Example
package main

import (
	"fmt"
	"github.com/miyamo2/xtract"
	"strings"
)

func main() {
	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

func (MapExtractor[K, V]) ByValue

func (x MapExtractor[K, V]) ByValue(condition func(V) bool) Extractor[K, V]

ByValue See: Extractor.ByValue

Example
package main

import (
	"fmt"
	"github.com/miyamo2/xtract"
)

func main() {
	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

func (MapExtractor[K, V]) Values added in v0.2.0

func (x MapExtractor[K, V]) Values() iter.Seq[V]

Values See: Extractor.Values

type SliceExtractor

type SliceExtractor[V any] struct {
	// contains filtered or unexported fields
}

SliceExtractor is implementation of Extractor for slice.

func (*SliceExtractor[V]) ByKey

func (x *SliceExtractor[V]) ByKey(condition func(int) bool) Extractor[int, V]

ByKey See: Extractor.ByKey

Example
package main

import (
	"fmt"
	"github.com/miyamo2/xtract"
)

func main() {
	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:

iterator
range over func

func (*SliceExtractor[V]) ByKeyAndValue

func (x *SliceExtractor[V]) ByKeyAndValue(condition func(int, V) bool) Extractor[int, V]

ByKeyAndValue See: Extractor.ByKeyAndValue

Example
package main

import (
	"fmt"
	"github.com/miyamo2/xtract"
)

func main() {
	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

func (*SliceExtractor[V]) ByValue

func (x *SliceExtractor[V]) ByValue(condition func(V) bool) Extractor[int, V]

ByValue See: Extractor.ByValue

Example
package main

import (
	"fmt"
	"github.com/miyamo2/xtract"
)

func main() {
	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

func (*SliceExtractor[V]) Values added in v0.2.0

func (x *SliceExtractor[V]) Values() iter.Seq[V]

Values See: Extractor.Values

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL