gent

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 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

Overview

Package gent contains miscellaneous tools that the author keeps rewriting in various projects.

Index

Examples

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.

Example
filterOdd := func(i int) bool { return i%2 != 0 }
fmt.Print(
	Filter(
		[]int{1, 2, 3, 4, 5},
		filterOdd))
Output:
[1 3 5]

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.

Example
fmt.Print(
	Map(
		[]int{1, 2, 4},
		func(i int) string {
			return fmt.Sprintf("item: %d", i)
		}))
Output:
[item: 1 item: 2 item: 4]

func NewOption added in v0.7.0

func NewOption[T any](t T, options ...func(t *T)) T

NewOption is a general function to implement option pattern.

func OrPanic2

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

OrPanic2 returns function that returns value if err is nil, else panics with message. Useful for cases where failure should result in panic and you don't want to deal with the returned error.

Example
defer func() {
	if r := recover(); r != nil {
		fmt.Print(r)
	}
}()

divide := func(a, b int) (int, error) {
	if b == 0 {
		return 0, errors.New("can't divide with zero")
	}
	return a / b, nil
}

// The usual case where the call succeeds and you don't need to deal with the error.
result := OrPanic2(divide(10, 2))("oops")
fmt.Println(result)

// Failing case where you still don't have to deal with the error.
OrPanic2(divide(1, 0))("nope")
Output:
5
Message: nope. Error: can't divide with zero.

func ReadLines

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

ReadLines read all lines in file filep. Empty lines are included. Returned lines do not contain newlines at the end.

func Tri

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

Tri returns one of the two values based on the condition. I.e. this is a ternary "operator".

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 an initialized gent.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 gent.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 and wasn't added.

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. Alias for gent.Set.Has.

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

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

Count returns the number of items in the set. Alias for gent.Set.Len.

func (*Set[T]) Equal

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

Equal returns true when the sets contain the exact same items.

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. Use gent.ForEachAll if there's no need to stop iteration.

func (*Set[T]) ForEachAll added in v0.3.0

func (v *Set[T]) ForEachAll(f func(each T))

ForEachAll iterates all items in the set and calls f for each item. Use gent.ForEach if you need to stop iteration.

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.

Directories

Path Synopsis
Package assfs contains filesystem assert functionality.
Package assfs contains filesystem assert functionality.
Package snap contains snapshot test functionality.
Package snap contains snapshot test functionality.

Jump to

Keyboard shortcuts

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