basicalter

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"foo", "bar", "foo"}
	output := basicalter.UniqueInSlice(input)
	fmt.Printf("%v", output)
}
Output:

[foo bar]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsoluteInt

func AbsoluteInt(num int) int

AbsoluteInt return absolute value of integer.

If the 'num' is the minimum value of integers, the function panic due to the lack of a corresponding positive value with integer type.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := -10
	output := basicalter.AbsoluteInt(input)
	fmt.Printf("%d", output)
}
Output:

10

func CutPrefixInString added in v0.8.0

func CutPrefixInString(s *string, prefix string) bool

CutPrefixInString rewrites the value of 's' without the provided leading 'prefix' string and reports whether it found the prefix.

If 's' is nil or if its value doesn't start with 'prefix', CutPrefixInString doesn't rewrite the value of 's' and returns false.

If 'prefix' is the empty string, CutPrefixInString returns true without rewriting.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	str := "foobar"
	basicalter.CutPrefixInString(&str, "foo")
	fmt.Println(str)
}
Output:

bar

func CutSuffixInString added in v0.8.0

func CutSuffixInString(s *string, suffix string) bool

CutSuffixInString rewrites the value of 's' without the provided ending 'suffix' string and reports whether it found the suffix.

If 's' is nil or if its value doesn't end with 'suffix', CutSuffixInString doesn't rewrite the value of 's' and returns false.

If 'suffix' is the empty string, CutSuffixInString returns true without rewriting.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	str := "foobar"
	basicalter.CutSuffixInString(&str, "bar")
	fmt.Println(str)
}
Output:

foo

func DelEmptyStrings

func DelEmptyStrings(list []string) []string

DelEmptyStrings remove empty elements in slice of string and return the result with a new slice.

func DelInSlice added in v0.6.0

func DelInSlice[T comparable, S ~[]T](elem T, list S) S

DelInSlice remove all occurrence of an element in a slice and return the result with a new slice.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"foo", "baz", "bar"}
	output := basicalter.DelInSlice("baz", input)
	fmt.Printf("%v", output)
}
Output:

[foo bar]

func DelRuneInStringWith added in v0.9.0

func DelRuneInStringWith(s *string, filtersOut ...func(rune) bool)

DelRuneInStringWith rewrites the value of 's' without the rune(s) that return true with one of the 'filtersOut' functions.

If 's' is nil, DelRuneInStringWith is a no-op.

Example
package main

import (
	"fmt"
	"unicode"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	str := "foo 1 bar"
	basicalter.DelRuneInStringWith(&str, unicode.IsSpace, unicode.IsDigit)
	fmt.Println(str)
}
Output:

foobar

func FilterInSliceWith added in v0.6.0

func FilterInSliceWith[T any, S ~[]T](list S, filter func(T) bool) S

FilterInSliceWith generate a new slice by applying on an input slice 'list' a function 'filter' to determine the inclusion of each element.

If 'list' is nil, FilterInSliceWith return nil.

If 'filter' is nil, FilterInSliceWith return list.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"foo", "baz", "bar", "baz"}
	output := basicalter.FilterInSliceWith(input, func(s string) bool {
		return strings.HasPrefix(s, "ba")
	})
	fmt.Printf("%v", output)
}
Output:

[baz bar baz]

func FilterRuneInStringWith added in v0.9.0

func FilterRuneInStringWith(s *string, filter func(rune) bool)

FilterRuneInStringWith rewrites the value of 's' with the rune(s) that return true with the function 'filter'.

If 's' or 'filter' is nil, FilterRuneInStringWith is a no-op.

Example
package main

import (
	"fmt"
	"unicode"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	str := "foo 1 bar"
	basicalter.FilterRuneInStringWith(&str, unicode.IsLetter)
	fmt.Println(str)
}
Output:

foobar

func MergeMaps added in v0.7.0

func MergeMaps[K comparable, V any, M ~map[K]V](dst M, overwrite bool, srcs ...M)

MergeMaps copies all key/value pairs in 'srcs' maps to 'dst' map. Set 'overwrite' to true to allow copy value when the key is already set in 'dst' or previous element of 'srcs'.

If 'dst' is nil, MergeMaps is a no-op.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	m1 := map[string]string{"foo": "baz"}
	m2 := map[string]string{"bar": "baz"}
	basicalter.MergeMaps(m1, false, m2)
	fmt.Printf("%+v", m1)
}
Output:

map[bar:baz foo:baz]

func ReplaceInSliceWith added in v0.6.0

func ReplaceInSliceWith[T any](list []T, replace func(T) T)

ReplaceInSliceWith replace each element of a slice with the result of the function 'replace' passed in arguments.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"Foo", "Bar", "Baz"}
	basicalter.ReplaceInSliceWith(input, strings.ToLower)
	fmt.Printf("%v", input)
}
Output:

[foo bar baz]

func ReverseSlice deprecated added in v0.6.0

func ReverseSlice[T any](list []T)

ReverseSlice reverse order of a slice last to first, before last to second, etc.

Deprecated: use slices.Reverse() from the standard 'slices' library instead.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"foo", "baz", "bar"}
	basicalter.ReverseSlice(input)
	fmt.Printf("%v", input)
}
Output:

[bar baz foo]

func SortStringsByLengthDec

func SortStringsByLengthDec(list []string)

SortStringsByLengthDec sort slice of string by length decreasing before lexicographic order.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"a1", "a10", "a11", "a100", "a2", "a3"}
	basicalter.SortStringsByLengthDec(input)
	fmt.Printf("%v", input)
}
Output:

[a100 a10 a11 a1 a2 a3]

func SortStringsByLengthInc

func SortStringsByLengthInc(list []string)

SortStringsByLengthInc sort slice of string by length increasing before lexicographic order.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"a1", "a10", "a11", "a100", "a2", "a3"}
	basicalter.SortStringsByLengthInc(input)
	fmt.Printf("%v", input)
}
Output:

[a1 a2 a3 a10 a11 a100]

func UniqueInSlice added in v0.6.0

func UniqueInSlice[T comparable, S ~[]T](list S) S

UniqueInSlice remove duplicate elements in slice and return the result with a new slice.

Example
package main

import (
	"fmt"

	"github.com/jeremmfr/go-utils/basicalter"
)

func main() {
	input := []string{"foo", "bar", "foo"}
	output := basicalter.UniqueInSlice(input)
	fmt.Printf("%v", output)
}
Output:

[foo bar]

Types

This section is empty.

Jump to

Keyboard shortcuts

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