utils

package
v0.0.0-...-ff01a36 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Empty     = ""
	Space     = " "
	Comma     = ","
	Colon     = ":"
	Semicolon = ";"
	Dot       = "."

	Backquote   = "`"
	Quote       = "\""
	SingleQuote = "'"
	Tilde       = "~"

	Ampersat   = "@"
	Hash       = "#"
	Dolar      = "$"
	Percent    = "%"
	Circumflex = "^"
	Ampersand  = "&"
	Asterisk   = "*"

	// Pair characters
	LeftParenthesis  = "("
	RightParenthesis = ")"
	OpenBrace        = "{"
	CloseBrace       = "}"
	OpenBracket      = "["
	CloseBracket     = "]"

	// Arithmetic operations
	Equal          = "="
	LessThan       = "<"
	GreaterThan    = ">"
	Plus           = "+"
	Minus          = "-"
	Division       = "/"
	Multiplication = "*"
	Modulus        = "%"

	Slash     = "/"
	Backslash = "\\"
	Pipe      = "|"

	ExclamationMark   = "!"
	IntegorrationMark = "?"

	NewLine = "\n"
)

Variables

This section is empty.

Functions

func GetOrPanic

func GetOrPanic[T any](res T, err error) T

func GetOrPanicF

func GetOrPanicF[T any](f func() (T, error)) func() T

func IfElse

func IfElse[T any](cond bool, a T, b T) T

Returns a if the evaluated condition cond is truthy. Returns b instead. Useful when conditionally get a certain value in one line.

age := 18
// Returns "coke" because condition is false
drink := IfElse(age > 21, "beer", "coke")

func JSONUnmarshalTilEOF

func JSONUnmarshalTilEOF(r io.ReadCloser, target interface{}) error

JSON Unmarshals the given ReadCloser buffer into target ensuring that the buffer is read until EOF.

func MergeMaps

func MergeMaps[T comparable, K any](src map[T]K, dst map[T]K) map[T]K

MergeMaps function merges two maps moving the src keys and values to the dst ones, without overriden them.

import "fmt"

src, dst := map[string]string { "a": "Hello", "b": "Bye" }, map[string]string { "a": "Salut", "c": "Aurevoir" }

MergeMaps(src, dst)

fmt.Println(dst) // Prints: map[a:Salut b:Bye c:Aurevoir]

func RemoveDuplicates

func RemoveDuplicates[T constraints.Ordered](input []T) []T

RemoveDuplicates is a simple function to remove duplicates from an ordered array.

input := []PersonalString{"hello", "bye", "hello"}

fmt.Println(RemoveDuplicates(input)) // output hello and bye

func RemoveDuplicatesKeepingFirstSeen

func RemoveDuplicatesKeepingFirstSeen[K constraints.Ordered, V any](
	input []V,
	mapFunc func(V) K,
) []V

RemoveDuplicatesKeepingLastSeen function parses an array of any structure and returns another array removing duplicates keeping inside of it the first ones. mapFunc allows us to get a key from the structure, so we can create the map with some kind of order. the key must be an ordered value.

type (
	PersonalString string
	Person struct {
		ID PersonalString
		Name string
		Age int
	}
)

input := []Person{
	{ ID: "1", Name: "Pedro", Age: 18 },
	{ ID: "2", Name: "Juan", Age: 30 },
	{ ID: "3", Name: "Diego", Age: 35 },
	{ ID: "2", Name: "Ander", Age: 24 },
}

output := RemoveDuplicatesKeepingFirstSeen(input, func(person Person) PersonalString {
	return person.ID
})

fmt.Println(output) // contains Pedro, Diego and Juan

func RemoveDuplicatesKeepingLastSeen

func RemoveDuplicatesKeepingLastSeen[K constraints.Ordered, V any](
	input []V,
	mapFunc func(V) K,
) []V

RemoveDuplicatesKeepingLastSeen function parses an array of any structure and returns another array removing duplicates keeping inside of it the last ones. mapFunc allows us to get a key from the structure, so we can create the map with some kind of order. the key must be an ordered value.

type (
	// This is used just to show that we don't need a strict int, string, float or complex type to used as a key.
	PersonalString string
	Person struct {
		ID PersonalString
		Name string
		Age int
	}
)

input := []Person{
	{ ID: "1", Name: "Pedro", Age: 18 },
	{ ID: "2", Name: "Juan", Age: 30 },
	{ ID: "3", Name: "Diego", Age: 35 },
	{ ID: "2", Name: "Ander", Age: 24 },
}

output := RemoveDuplicatesKeepingLastSeen(input, func(person Person) PersonalString {
	return person.ID
})

fmt.Println(output) // contains Pedro, Diego and Ander

func SimilarString

func SimilarString[U []T, T ~string](input U) []string

func UniqueValues

func UniqueValues(strSlice []string) []string

Types

type Predicate

type Predicate[T any] func(T) bool

type Reduce

type Reduce[T any] func(T, T) T

func IfElseCond

func IfElseCond[T any](cond Predicate[T]) Reduce[T]

Returns a function with the condition preloaded, so you don't have to call it over and over again.

ifEmptyOrElse := IfElseCond(func(a string) bool {
	return strings.TrimSpace(a) != ""
})

Expect(ifEmptyOrElse("", "second")).To(Equal("second"))
Expect(ifEmptyOrElse("first", "second")).To(Equal("first"))

type Set

type Set[T comparable] map[T]any

func NewSet

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

func (Set[T]) Add

func (s Set[T]) Add(input T)

func (Set[T]) Exists

func (s Set[T]) Exists(input T) bool

func (Set[T]) ToArr

func (s Set[T]) ToArr() []T

Jump to

Keyboard shortcuts

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