gana

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

gana

gana is a special small library for generifying golang code. Written in close collaboration with Matthew Sanetra.

Installation

go get github.com/thecsw/gana

Usage

See generic functions included in the library in ops.go.

Set operations included in set.go.

Tuple operations included in tuple.go.

String operations included in string.go.

Workers included in worker.go. But I recommend using komi instead.

Lazy operations included in lazy.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T comparable](val T, arr []T) bool

All returns true if all elements in the list match the given value.

func Allf

func Allf[T any](foo func(v T) bool, arr []T) bool

Allf returns true if all elements in the array return true when passed to foo.

func Any

func Any[T comparable](val T, arr []T) bool

Any returns true if any element in the list matches the given value.

func Anyf

func Anyf[T any](foo func(v T) bool, arr []T) bool

Anyf returns true if any elemens in the list returns true when passed to foo.

func Drop

func Drop[T any, U constraints.Unsigned](num U, arr []T) []T

Drop allocates a new slice, with the first `num` elements dropped.

func DropRunes

func DropRunes[T constraints.Unsigned](num T, what string) string

DropRunes allocates a new string, with the first `num` runes of a string dropped.

func DropString

func DropString[T constraints.Unsigned](num T, what string) string

DropString allocates a new string, with the first `num` bytes of a string dropped.

func Equals

func Equals[T comparable](a, b T) bool

Equals is the default equals function for any ordered type and will return true if a = b, false otherwise.

func EqualsClosure

func EqualsClosure[T comparable](a T) func(T) bool

Equals is the default equals function that will return a closure equals function to compare the value to the original wrapped val.

func Filter

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

Filter calls the function and returns list of values that returned true.

func First

func First[T any](x []T) T

First returns the first element of the given array, zero value otherwise.

func GenericWorker

func GenericWorker[A, B any](
	inputs <-chan A,
	outputs chan<- B,
	work func(A) B,
	wg *sync.WaitGroup,
)

GenericWorker gets values from `inputs channel`, performs `work` on the value and puts it into the `outputs` channel, while also marking itself as `Done` in `WaitGroup` when `inputs` channel is closed.

func GenericWorkers

func GenericWorkers[A, B any](
	inputs <-chan A,
	work func(A) B,
	workers int,
	cz int,
) <-chan B

GenericWorkers spins up given number of `genericWorker` routines that all perform `work` and it itself returns the `outputs` channel with the buffer defined by `cz`; do close the `inputs` channel when there is no more work left for workers to safely exit their goroutines.

func GetPointer

func GetPointer[T any](val T) *T

GetPointer returns the pointer of a given lhs.

func Greater

func Greater[T constraints.Ordered](a, b T) bool

DefaultLess is the default greater function for any ordered type and will return true if a > b, false otherwise.

func Last

func Last[T any](x []T) T

Last returns the last element of the given array, zero value otherwise.

func Less

func Less[T constraints.Ordered](a, b T) bool

Less is the default less function for any ordered type and will return true if a < b, false otherwise.

func Map

func Map[T, S any](f func(T) S, arr []T) []S

Map calls the function on every array element and returns results in list.

func Max

func Max[T constraints.Ordered](a, b T) T

Max returns the maximum of two numbers.

func Maxf

func Maxf[T any](less func(T, T) bool, vals ...T) T

Maxf returns the maximum of the given values with a given "less" comparator, or if no values are given, the zero value of the type.

func Maxv

func Maxv[T constraints.Ordered](vals ...T) T

Maxv returns the maximum of the given values, or if no values are given, the zero value of the type.

func Min

func Min[T constraints.Ordered](a, b T) T

Min returns the minimum of two numbers.

func MinMaxf

func MinMaxf[T any](less func(T, T) bool, vals ...T) (T, T)

MinMaxv returns the minimum and maximum of the given values with a given "less" comparator, or if no values are given, two zero value of the type.

func MinMaxv

func MinMaxv[T constraints.Ordered](vals ...T) (T, T)

MinMaxv returns the minimum and maximum of the given values, or if no values are given, two zero value of the type.

func Minf

func Minf[T any](less func(T, T) bool, vals ...T) T

Minf returns the minimum of the given values with a given "less" comparator, or if no values are given, the zero value of the type.

func Minv

func Minv[T constraints.Ordered](vals ...T) T

Minv returns the minimum of the given values, or if no values are given, the zero value of the type.

func PeekString

func PeekString[T constraints.Unsigned](num T, what string) string

PeekString takes the first `num` bytes of a string by slicing (underlying array unaffected).

func RemoveIf

func RemoveIf[T any](f func(T) bool, arr []T) []T

RemoveIf calls the function and returns list of values that returned false.

func Repeat

func Repeat[T any](val T, size int) []T

Repeat creates a list of given size consisting of the same given value.

func Skip

func Skip[T any, U constraints.Unsigned](num U, arr []T) []T

Skips skips the first `num` elements by slicing (underlying array unaffected).

func SkipRunes

func SkipRunes[T constraints.Unsigned](num T, what string) string

SkipRunes skips the first `num` runes of a string by slicing (underlying array unaffected).

func SkipString

func SkipString[T constraints.Unsigned](num T, what string) string

SkipString skips the first `num` bytes of a string by slicing (underlying array unaffected).

func Tail

func Tail[T any](num int, arr []T) []T

Tail returns up to the last `num` elements.

func Take

func Take[T any](num int, arr []T) []T

Take returns up to the first `num` elements.

func TakeString

func TakeString[T constraints.Unsigned](num T, what string) string

TakeString allocates a new string, with the first `num` bytes of a string taken.

func ZeroValue

func ZeroValue[T any]() T

ZeroValue returns the zero value of any type.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T comparable]() *Set[T]

NewSet returns a new set.

func (*Set[T]) Add

func (s *Set[T]) Add(v T)

Add adds a value to the set.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear removes all elements from the set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(v T) bool

Contains returns true if the set contains the value.

func (*Set[T]) Difference

func (s *Set[T]) Difference(other *Set[T]) *Set[T]

Difference returns a new set with elements that are in s but not in other.

func (*Set[T]) Intersection

func (s *Set[T]) Intersection(other *Set[T]) *Set[T]

Intersection returns a new set with elements that are both in s and other.

func (*Set[T]) IsDisjoint

func (s *Set[T]) IsDisjoint(other *Set[T]) bool

IsDisjoint returns true if s and other have no elements in common.

func (*Set[T]) IsEqual

func (s *Set[T]) IsEqual(other *Set[T]) bool

IsEqual returns true if s and other contain the same elements.

func (*Set[T]) IsSubset

func (s *Set[T]) IsSubset(other *Set[T]) bool

IsSubset returns true if s is a subset of other.

func (*Set[T]) IsSuperset

func (s *Set[T]) IsSuperset(other *Set[T]) bool

IsSuperset returns true if s is a superset of other.

func (*Set[_]) Len

func (s *Set[_]) Len() int

Len returns the number of elements in the set.

func (*Set[T]) Remove

func (s *Set[T]) Remove(v T)

Remove removes a value from the set.

func (*Set[T]) SymmetricDifference

func (s *Set[T]) SymmetricDifference(other *Set[T]) *Set[T]

SymmetricDifference returns a new set with elements that are in either s or other but not both.

func (*Set[T]) Union

func (s *Set[T]) Union(other *Set[T]) *Set[T]

Union returns a new set with elements from both s and other.

func (*Set[T]) Values

func (s *Set[T]) Values() []T

Values returns all values in the set.

type Tuple

type Tuple[A, B any] struct {
	First  A
	Second B
}

Tuple is a generic tuple type.

func NewTuple

func NewTuple[A, B any](first A, second B) Tuple[A, B]

NewTuple returns a new tuple.

func Zip

func Zip[T, U any](a []T, b []U) []Tuple[T, U]

Zip returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument lists.

func (Tuple[A, B]) Unpack

func (t Tuple[A, B]) Unpack() (A, B)

Unpack returns the tuple's elements.

func (Tuple[A, B]) UnpackRef

func (t Tuple[A, B]) UnpackRef() (*A, *B)

UnpackRef returns references to the tuple's elements.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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