Documentation ¶
Index ¶
- Constants
- func GetOrPanic[T any](res T, err error) T
- func GetOrPanicF[T any](f func() (T, error)) func() T
- func IfElse[T any](cond bool, a T, b T) T
- func JSONUnmarshalTilEOF(r io.ReadCloser, target interface{}) error
- func MergeMaps[T comparable, K any](src map[T]K, dst map[T]K) map[T]K
- func RemoveDuplicates[T constraints.Ordered](input []T) []T
- func RemoveDuplicatesKeepingFirstSeen[K constraints.Ordered, V any](input []V, mapFunc func(V) K) []V
- func RemoveDuplicatesKeepingLastSeen[K constraints.Ordered, V any](input []V, mapFunc func(V) K) []V
- func SimilarString[U []T, T ~string](input U) []string
- func UniqueValues(strSlice []string) []string
- type Predicate
- type Reduce
- type Set
Constants ¶
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 GetOrPanicF ¶
func IfElse ¶
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 UniqueValues ¶
Types ¶
type Reduce ¶
type Reduce[T any] func(T, T) T
func IfElseCond ¶
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"))