util

package
v0.0.0-...-f8e1c36 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: Unlicense Imports: 19 Imported by: 0

Documentation

Overview

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

TODO:

  • loading checkpoint
  • option for output filename

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

func CollatzStoppingTime(i int) int

CollatzStoppingTime returns the number of steps in the Collatz (3n+1) sequence before reaching 1.

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 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[T any](s []T) [][]T

Permutations returns all permutations of the elements of s. If the elements are not unique, then the return value will 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 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

type ModeSelector

type ModeSelector struct {
	// contains filtered or unexported fields
}

ModeSelector reads argv[1] It is not safe for concurrent access from multiple goroutines.

func NewModeSelector

func NewModeSelector() *ModeSelector

NewModeSelector creates a ModeSelector.

func (*ModeSelector) Add

func (ms *ModeSelector) Add(name string, f func() error)

Add defines a new mode.

func (*ModeSelector) Run

func (ms *ModeSelector) Run() error

type Option

type Option func(*WorkQueue)

func SetCheckpointInterval

func SetCheckpointInterval(d time.Duration) Option

SetCheckpointInterval sets the interval at which a work checkpoint is written to disk.

func SetNumWorkers

func SetNumWorkers(i int) Option

SetNumWorkers sets the number of goroutines that are concurrently doing work.

type WorkQueue

type WorkQueue struct {
	// contains filtered or unexported fields
}

WorkQueue stores a list of tasks to complete, hands each task to a user-provided function, and writes any "successes", as defined by the user-provided function, to disk. It checkpoints its progress along the way so that it can be stopped and restarted from a checkpoint while needing minimal work to be redone.

func NewFromCheckpoint

func NewFromCheckpoint(fname string, options ...Option) *WorkQueue

NewFromCheckpoint creates a WorkQueue from a checkpoint file that an earlier WorkQueue has written.

func NewWorkQueue

func NewWorkQueue(items []string, f func(string, chan<- string), options ...Option) *WorkQueue

NewWorkQueue creates a WorkQueue with a list of work to perform.

func (*WorkQueue) Run

func (wq *WorkQueue) Run()

Run starts the work and blocks until it is completed.

Jump to

Keyboard shortcuts

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