util

package
v0.0.0-...-9121054 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Unlicense Imports: 14 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

View Source
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 Alphabet

func Alphabet() iter.Seq[rune]

Alphabet returns an iter.Seq over all lowercase letters.

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

func CollatzStoppingTime(i int) int

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

func Explode(vals ...[]int) iter.Seq[[]int]

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

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/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

func IsPrime(n int) bool

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 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 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/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 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 SHA256

func SHA256(s string) string

SHA256 returns the SHA-256 hash of the input as a hex-encoded string.

func SortLetters

func SortLetters(s string) string

SortLetters sorts the letters of ASCII strings. SortLetter("asdf") == "adfs". This is useful for finding anagrams.

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