Documentation
¶
Overview ¶
Package util is a collection of utilities for manipulating files, retrieving web pages, basic math, and other useful things.
Index ¶
- func A1Decode(n int) rune
- func A1Encode[T constraints.Integer](n T) int
- func AsSet[T comparable](vals []T) map[T]bool
- func CBF(s string) []int
- func Combinations(length int) [][]int
- func Digits[T constraints.Integer](n T) []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 Histogram(s string) map[rune]int
- func IsPrime(n int) bool
- func IsUnique[T comparable](vals ...T) bool
- func Must(err error)
- func MustBool(b bool)
- func Permutations(s []int) [][]int
- func PrintAscending[T cmp.Ordered, U any](m map[T]U)
- func ROT(n int, w string) string
- func ReadLines(file string) []string
- func ResponseCode(url string) int
- func RuneCount(s string) map[rune]int
- func ToCoord(digits []int) string
- func Wget(url string) []byte
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func A1Decode ¶
A1Decode decodes a number in the range [1-26] using the A=1, ..., Z=26 substitution cipher.
func A1Encode ¶
func A1Encode[T constraints.Integer](n T) int
A1Encode encodes a rune in the range [A-Za-z] using the A=1, ..., Z=26 substitution cipher.
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 CBF ¶
CBF encodes a string into a slice of integers. CBF encoding is similar to A1Encode, but done mod 10.
func Combinations ¶
Combinations returns all combinations of the digits 1-9 of the specified length. Returned combinations are unique up to ordering (if [1, 2] is included, [2, 1] will not be). Combinations(2) returns [[1, 1], [1, 2], ..., [8, 9], [9, 9]] (45 elements). TODO: generalize to support a supplied alphabet.
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/util"
)
func main() {
fmt.Println(util.Digits(1234))
}
Output: [1 2 3 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/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/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 Permutations ¶
Permutations returns all permutations of the elements of s. If the elements are not unique, then the return value with contain duplicates.
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/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 ROT ¶
ROT rotates w by n letter. ROT(13, "terra") = "green". Currently only handles lowercase letters.
func ResponseCode ¶
Types ¶
This section is empty.