utils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: BSD-2-Clause Imports: 1 Imported by: 0

README

Utils for Golang

A collection of utilities (functions, types ...) for Golang with generics.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[InType, OutType any](f func(OutType, InType) OutType, init OutType, s []InType) OutType

Accumulate traverses s and accumulates results of function f with initial value init Accumulate(f, init, {a1, ..., bn}) -> f( ... f(f(init, a1), a2) ..., an)

func Combine

func Combine[T1, T2, T3 any](s1 []T1, s2 []T2, zipper func(T1, T2) T3) []T3

Combine combines elements of s1 and s2 using zipper function returned slice of the length of the smallest input

func Exists

func Exists[T any](f func(T) bool, s []T) bool

Exists returns true if f returns true for at least one elements of s

func Filter

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

Filter returns all the elements of s for which f returns true

func ForAll

func ForAll[T any](f func(T) bool, s []T) bool

ForAll returns true if f applies to all elements of s returns true

func Insert

func Insert[T any](x T, pos int, s []T) []T

Insert inserts x as position pos in s if pos >= len(s) insert at the end of s panic if pos is less than 0

func Iter

func Iter[T any](s []T, f func(T))

Iter applies f to all elements of s

func LowerBound

func LowerBound[T constraints.Ordered](x T, s []T) int

LowerBound returns the position of the first elements that is not smaller than x LowerBound returns a coherent result only if s is sorted

func Map

func Map[InType, OutType any](in []InType, f func(InType) OutType) []OutType

Map converts a slice of InType into a slice of OutType using function f

func OptionalFilter

func OptionalFilter[T1, T2 any](f func(T1) Option[T2], s []T1) []T2

OptionalFilter returns the slice made of f(e) for all e in s such that f(e).HasValue() is true

func Partition

func Partition[T constraints.Ordered](x T, s []T) int

Partition moves elements to form a partition and returns the position of the pivot Let p := Partition(x, s) then for all i < p s[i] < x and for all i >= p, s[i] >= x

func PartitionFilter

func PartitionFilter[T any](f func(T) bool, s []T) int

PartitionFilter moves elements to form a partition w.r.t to f Let p := PartitionFilter(f, s) then for all i < p, f([i]) is true and for all i >= p f(s[i]) is false

func Reverse

func Reverse[T any](s []T)

Reverse reverse elements in s

Types

type Option

type Option[T any] interface {
	HasValue() bool
	Value() T
	Do(func(T)) Option[T]
	Else(func()) Option[T]
}

func OptionalDo

func OptionalDo[T any](o Option[T], f func(T)) Option[T]

OptionalDo calls f(o.Value()) if o is not empty and returns o in all cases

func OptionalElse

func OptionalElse[T any](o Option[T], f func()) Option[T]

OptionalElse calls f if o is empty and returns o in all cases

func OptionalFlatMap

func OptionalFlatMap[T1, T2 any](o Option[T1], f func(T1) Option[T2]) Option[T2]

OptionalFlatMap returns the result of f(o.Value()) if o has a value, returns an empty optional otherwise Similar to OptionalMap but for function that already returns an optional type

func OptionalMap

func OptionalMap[T1, T2 any](o Option[T1], f func(T1) T2) Option[T2]

OptionalMap returns an option value result from f(o.Value()) if o is not empty, returns an empty optional otherwise

type OptionImplem

type OptionImplem[T any] struct {
	// contains filtered or unexported fields
}

OptionImplem model an optional value

func NewOption

func NewOption[T any](v T) *OptionImplem[T]

NewOption returns an optional value containing v

func NilOption

func NilOption[T any]() *OptionImplem[T]

NilOption returns an empty optional value

func (*OptionImplem[T]) Do

func (o *OptionImplem[T]) Do(f func(T)) Option[T]

Do calls f(o.Value()) if o is not empty and returns o in all cases

func (*OptionImplem[T]) Else

func (o *OptionImplem[T]) Else(f func()) Option[T]

Else calls f if o is empty and returns o in all cases

func (*OptionImplem[T]) HasValue

func (o *OptionImplem[T]) HasValue() bool

HasValue returns true if the optional has a value

func (*OptionImplem[T]) Value

func (o *OptionImplem[T]) Value() T

Value returns the content of the optional value, panic if empty

Jump to

Keyboard shortcuts

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