collfunc

package module
v0.0.0-...-1ed9d87 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2022 License: MIT Imports: 3 Imported by: 0

README

collfunc

Iterate, Compare, Filter, Map, etc... collection (arrays, slices, maps and custom types) functions!

func main() {
	s := []int{0, 1, 2, 3, 4, 5}
	iter := collfunc.Iterate[int](s)
	for v, ok := iter(); ok; v, ok = iter() {
	    fmt.Println(v)
	}
}

Output:

0
1
2
3
4
5

Zip:

func main() {
	first := []int{0, 2, 4, 6}
	second := []int{1, 3, 5, 7}
	fmt.Println(collfunc.ToSlice[int](collfunc.Zip[int](first, second)))
}

Output:

[0, 1, 2, 3, 4, 5, 6, 7]

Support for custom collections:

type customIterable int

func (ci customIterable) Iterator() func() (int, bool) {
	i := 0

	return func() (int, bool) {
		if i < int(ci) {
			i++
			return i - 1, true
		}

		return 0, false
	}
}

func main() {
	var ci customIterable = 5
	if collfunc.Compare[int](ci, []int{0, 1, 2, 3, 4}) != 0 {
		panic(":(")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[T any](collection any, f func(cur, mem T) T) T

Accumulate calculation on collection items.

func Compare

func Compare[T constraints.Ordered](first, second any) int

Compare two collections, negative if first is bigger, positive if second, zero for equality.

func Concat

func Concat[T any](collections ...any) func() (T, bool)

Concat returns an iterator that iterates over all the collections.

func CreateMap

func CreateMap[K comparable, V any](keys any, values any) map[K]V

CreateMap initializes and returns a new map with keys from the keys collection and corresponding values from the values collection

func Empty

func Empty[T any]() func() (T, bool)

Empty returns an iterator that always returns the zero value of T and false.

func Enumerate

func Enumerate[T any](collection any) func() (int, T, bool)

Enumerate returns an indexed iterator for the given collection.

func Filter

func Filter[T any](collection any, f func(T) bool) func() (T, bool)

Filter filters a collection using a predicate, it will return the result as an iterator.

func In

func In[T comparable](item T, collection any) bool

In returns true if the given item is found within the collection, it returns false otherwise.

func Iterate

func Iterate[T any](collection any) func() (T, bool)

Iterate returns an iterator for iterating over the collection, which can be of type Slice, Array, Map, Iterable or iterator (func() (T, bool), will be returned as-is).

func Map

func Map[T, S any](collection any, f func(T) S) func() (S, bool)

Map every item in the collection using the mapping function, returns an iterator as the result.

func One

func One[T any](collection any) T

func Sink

func Sink[T, S any](f func(values ...T) S, collections ...any) func() (S, bool)

Sink maps values from multiple collections into a single collection, returned as an iterator.

func ToChan

func ToChan[T any](collection any) <-chan T

ToChan writes all given collection items to a newly created channel

func ToMap

func ToMap[K comparable, V any](collection any, f func(K) V) map[K]V

ToMap initializes and returns a new map from the given collection, a mapping function is called to map a value for every key

func ToSlice

func ToSlice[T any](collection any) []T

ToSlice initializes and returns a new slice from the given collection

func Zip

func Zip[T any](first, second any) func() (T, bool)

Zip returns an iterator that iterates over the two collections in a zipped order.

Types

type Iterable

type Iterable[T any] interface {
	// Iterator returns an interator in the form
	// of a function that returns the next value
	// and a second boolean that signals the end
	// of the iteration.
	Iterator() func() (T, bool)
}

Iterable types that implement this interface can be used with all the funcs in this package.

Jump to

Keyboard shortcuts

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