util

package
v0.0.0-...-b56195b Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: Unlicense Imports: 12 Imported by: 0

Documentation

Overview

Package util is a collection of utilities for manipulating files, retrieving web pages, basic math, and other useful things.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func A1Decode

func A1Decode(n int) rune

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

func CBF(s string) []int

CBF encodes a string into a slice of integers. CBF encoding is similar to A1Encode, but done mod 10.

func Combinations

func Combinations(length int) [][]int

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

func Factor(n int) []int

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 Histogram

func Histogram(s string) map[rune]int

Histogram produces a count of all of the letters that appear in s.

func IsPrime

func IsPrime(n int) bool

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 Must

func Must(err error)

Must prints err and exits if err is not nil.

func MustBool

func MustBool(b bool)

MustBool exits if b is false.

func Permutations

func Permutations(s []int) [][]int

Permutations returns all permutations of the elements of s. If the elements are not unique, then the return value with contain duplicates.

func PrintAscending

func PrintAscending[T cmp.Ordered, U any](m map[T]U)

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

func ROT(n int, w string) string

ROT rotates w by n letter. ROT(13, "terra") = "green". Currently only handles lowercase letters.

func ReadLines

func ReadLines(file string) []string

ReadLines opens the named file and returns a slice of the lines of the file.

func ResponseCode

func ResponseCode(url string) int

func RuneCount

func RuneCount(s string) map[rune]int

RuneCount returns a map containing each rune in s and how many times it occurs.

func ToCoord

func ToCoord(digits []int) string

func Wget

func Wget(url string) []byte

Wget fetches the named URL and returns its contents. It exits on any error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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