Documentation
¶
Overview ¶
Package gent contains miscellaneous tools that the author keeps rewriting in various projects.
Index ¶
- func Filter[T any](s []T, f func(T) bool) []T
- func Map[T any, U any](s []T, f func(T) U) []U
- func NewOption[T any](t T, options ...func(t *T)) T
- func OrPanic2[T any](value T, err error) func(message string) T
- func ReadLines(filep string) (lines []string, err error)
- func Tri[T any](condition bool, a, b T) T
- type Pair
- type Set
- func (v *Set[T]) Add(item T) (added bool)
- func (v *Set[T]) Clear()
- func (v *Set[T]) Contains(item T) bool
- func (v *Set[T]) Count() int
- func (v *Set[T]) Equal(s *Set[T]) bool
- func (v *Set[T]) ForEach(f func(each T, stop func()))
- func (v *Set[T]) ForEachAll(f func(each T))
- func (v *Set[T]) Has(item T) bool
- func (v *Set[T]) Len() int
- func (v *Set[T]) Remove(item T) (existed bool)
- func (v *Set[T]) ToSlice() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
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 ¶
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 ¶
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.
Types ¶
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a naive map backed set.
func (*Set[T]) Add ¶
Add item to the set, return true if it was added. Otherwise it already existed and wasn't added.
func (*Set[T]) Contains ¶
Contains checks if item exists in the set. Alias for gent.Set.Has.
func (*Set[T]) Count ¶ added in v0.2.0
Count returns the number of items in the set. Alias for gent.Set.Len.
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.