gent

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2025 License: MIT Imports: 3 Imported by: 30

README

gent

Personal reusable code for my Go projects.

Name

Roughly: "denarced" -> "den" -> (this is for Go so "g") -> "gen" -> "gen" + "t(ools)" -> "gent".

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

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

Filter values in s with f. When f returns true, item is included in the response slice.

func Map

func Map[T any, U any](s []T, f func(T) U) []U

Map a slice into another slice of the same size.

func OrPanic2

func OrPanic2[T any](value T, err error) func(message string) T

OrPanic2 return function that returns value if err is nil, else panics with message.

func ReadLines

func ReadLines(filep string) (lines []string, err error)

ReadLines read all lines in file filep.

func Tri

func Tri[T any](condition bool, a, b T) T

Tri returns one of the two values based on the condition.

Types

type Pair

type Pair[T any, U any] struct {
	First  T
	Second U
}

Pair is a pair of values.

func NewPair

func NewPair[T any, U any](first T, second U) Pair[T, U]

NewPair create a Pair.

type Set

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

Set is a naive map backed set.

func NewSet

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

NewSet creates a new Set.

func (*Set[T]) Add

func (v *Set[T]) Add(item T) (added bool)

Add item to the set, return true if it was added. Otherwise it already existed.

func (*Set[T]) Clear

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

Clear the set, remove all items.

func (*Set[T]) Contains

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

Contains checks if item exists in the set.

func (*Set[T]) Count added in v0.2.0

func (v *Set[T]) Count() int

Count returns the number of items in the set.

func (*Set[T]) Equal

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

Equal returns true when sets are equivalent in content.

func (*Set[T]) ForEach

func (v *Set[T]) ForEach(f func(each T, stop func()))

ForEach iterates all items in the set, calls f for each item, stops if stop is called.

func (*Set[T]) Has

func (v *Set[T]) Has(item T) bool

Has checks if item exists in the set.

func (*Set[T]) Len

func (v *Set[T]) Len() int

Len returns the number of items in the set.

func (*Set[T]) Remove

func (v *Set[T]) Remove(item T) (existed bool)

Remove removes an item in the set, returns true if it was. I.e. if it existed.

func (*Set[T]) ToSlice

func (v *Set[T]) ToSlice() []T

ToSlice returns a slice with all set items. Set itself doesn't change.

Jump to

Keyboard shortcuts

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