Documentation
¶
Overview ¶
Package util is a collection of utilities for manipulating files, retrieving web pages, basic math, and other useful things.
Index ¶
- Variables
- func Alphabet() iter.Seq[rune]
- func AsSet[T comparable](vals []T) map[T]bool
- func CollatzStoppingTime(i int) int
- func Digits[T constraints.Integer](n T) []int
- func Explode(vals ...[]int) iter.Seq[[]int]
- func Factor(n int) []int
- func FromDigits[T constraints.Integer](digits []T) int
- func FromDigitsBase[T constraints.Integer](digits []T, base int) int
- func IsPrime(n int) bool
- func IsUnique[T comparable](vals ...T) bool
- func Must(err error)
- func MustBool(b bool)
- func PrintAscending[T cmp.Ordered, U any](m map[T]U)
- func ReadLines(file string) []string
- func ResponseCode(url string) int
- func RuneCount(s string) map[rune]int
- func SHA256(s string) string
- func SortLetters(s string) string
- func ToCoord(digits []int) string
- func Wget(url string) []byte
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var D = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
D contains the digits 0-9. It is a common input to Explode.
Functions ¶
func AsSet ¶
func AsSet[T comparable](vals []T) map[T]bool
AsSet returns a set representation of vals, as a map with bool values.
func CollatzStoppingTime ¶
CollatzStoppingTime returns the number of steps in the Collatz (3n+1) sequence before reaching 1.
func Digits ¶
func Digits[T constraints.Integer](n T) []int
Digits returns a slice of the digits of n. Digits(1234) returns [1 2 3 4].
Example ¶
package main
import (
"fmt"
"github.com/bitlux/caches/lib/util"
)
func main() {
fmt.Println(util.Digits(1234))
}
Output: [1 2 3 4]
func Explode ¶
Explode takes a list of options, where each option is a list of ints. It returns the cross product of all options. Explode({1, 2}, {3, 4}) = {{1, 3}, {1, 4}, {2, 3}, {2, 4}}
func Factor ¶
Factor returns the prime factors of n. n must be greater than 1.
Example ¶
package main
import (
"fmt"
"github.com/bitlux/caches/lib/util"
)
func main() {
fmt.Println(util.Factor(60))
}
Output: [2 2 3 5]
func FromDigits ¶
func FromDigits[T constraints.Integer](digits []T) int
FromDigits takes a slice of digits and returns them as a single number. It is the inverse of Digits.
func FromDigitsBase ¶
func FromDigitsBase[T constraints.Integer](digits []T, base int) int
FromDigitsBase takes a slice of digits in the provided base and returns them as a single number. It is the inverse of Digits.
func IsPrime ¶
IsPrime returns whether n is prime.
Example ¶
package main
import (
"fmt"
"github.com/bitlux/caches/lib/util"
)
func main() {
fmt.Println(util.IsPrime(101))
}
Output: true
func IsUnique ¶
func IsUnique[T comparable](vals ...T) bool
IsUnique returns whether the elements of vals are all unique.
func PrintAscending ¶
PrintAscending prints the keys and values in the map in increasing order of the keys. If it determines that each key in the map is a rune, it will print the rune using %c. Other values are printed with %v.
Example ¶
package main
import (
"github.com/bitlux/caches/lib/util"
)
func main() {
m1 := map[rune]int{
'A': 10,
}
util.PrintAscending(m1)
m2 := map[string]int{
"asdf": 10,
}
util.PrintAscending(m2)
}
Output: 'A': 10 asdf: 10
func ResponseCode ¶
func SortLetters ¶
SortLetters sorts the letters of ASCII strings. SortLetter("asdf") == "adfs". This is useful for finding anagrams.
Types ¶
This section is empty.