Documentation
¶
Index ¶
- func CountElements[T comparable](xs []T) map[T]int
- func Filter[T any](xs []T, f func(x T) bool) []T
- func ForEachLine(r io.Reader, stripEOL bool, strict bool, fn func(string) (bool, error)) error
- func IfElse[T any](b bool, consequent T, alternative T) T
- func MakePtr[T any](x T) *T
- func MakeRandomBytes(n uint64) ([]byte, error)
- func Map[T1 any, T2 any](xs []T1, f func(T1) T2) []T2
- func MergeSlices[T cmp.Ordered](xs []T, ys []T, isLessThan func(x, y T) bool) []T
- func MergeSortSlices[T cmp.Ordered](xs []T, isLessThan func(x1, x2 T) bool) []T
- func NewRand() (*rand.Rand, error)
- func Prompt(fin *bufio.Reader, fout *bufio.Writer, prompt string) (string, error)
- func PromptUnbuffered(prompt string) (string, error)
- func ReadLine(r *bufio.Reader) (string, error)
- func Reduce[T1 any, T2 any](xs []T1, init T2, f func(T2, T1) T2) T2
- func SliceToSet[T comparable](xs []T) map[T]int
- func StripEOL(line string) string
- func SubtractSet[T comparable](xs, ys map[T]int) map[T]int
- func SubtractSlice[T comparable](xs, ys []T) []T
- type Pair
- type SList
- func (l *SList[T]) Contains(f func(x T) bool) bool
- func (l *SList[T]) Drop(n uint64) *SList[T]
- func (l *SList[T]) DropUntil(f func(x T) bool) *SList[T]
- func (l *SList[T]) DropWhile(f func(x T) bool) *SList[T]
- func (l *SList[T]) Empty() bool
- func (xs *SList[T]) Equal(ys *SList[T], isEqual func(x, y T) bool) bool
- func (l *SList[T]) Head() (T, bool)
- func (l *SList[T]) Length() uint64
- func (xs *SList[T]) Merge(ys *SList[T], isLessThan func(x, y T) bool) *SList[T]
- func (l *SList[T]) MergeSort(isLessThan func(l1, l2 T) bool) *SList[T]
- func (l *SList[T]) Nth(n uint64) (T, bool)
- func (l *SList[T]) PushFront(value T) *SList[T]
- func (l *SList[T]) Reverse() *SList[T]
- func (l *SList[T]) String() string
- func (l *SList[T]) Tail() *SList[T]
- func (l *SList[T]) Take(n uint64) *SList[T]
- func (l *SList[T]) TakeUntil(f func(x T) bool) *SList[T]
- func (l *SList[T]) TakeWhile(f func(x T) bool) *SList[T]
- func (l *SList[T]) ToSlice() []T
- type Stack
- type Vector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountElements ¶
func CountElements[T comparable](xs []T) map[T]int
CountElements counts the number of times that element occurs in the slice.
func Filter ¶
Filter uses the function f to filter the input slice xs. The return slice will hold only the values of xs for which f returned true.
func ForEachLine ¶
ForEachLine invokes the function fn for each line of text in the io.Reader r.
If strict is true, this method uses [jrutil.ReadLine()] to get the next line which is slower but works with DOS, Mac, and UNIX EOL sequences. If strict is false (recommended), this method uses [bufio.ReadString()] which is over twice as fast and works with DOS and UNIX EOLs but not Mac EOLs.
If stripEOL is true, the EOLs will be stripped from the line before fn is called.
To receive the next line of text, fn must return (true, nil). If fn returns an error, it is forward to the caller as the error returned by ForEachLine(). If fn is translating input text and writing it back out, for best performance, fn should write to a bufio.Writer wrapper.
func MakePtr ¶
func MakePtr[T any](x T) *T
MakePtr is useful for making pointers from intrinsic types like int for which the address-of operator does not work.
func MakeRandomBytes ¶
MakeRandomBytes returns a slice of bytes initialzed from crypto.rand.Read().
func Map ¶
Map use the function f to map each element of the input slice xs to the corresponding element in the output slice.
func MergeSlices ¶
Merge merges the two sorted slices (xs and ys) and returns a new sorted slice that contains all of the elements from the two sorted input slices. This function performs a shallow copy when copying values from xs and ys to the result. Thus, if complex values are being sorted, xs and ys should hold pointers (for which shallow copying is correct).
func MergeSortSlices ¶
MergeSort returns a new, sorted slice based on the comparison function f.
func NewRand ¶
func NewRand() (*rand.Rand, error)
NewRand returns a new math.rand.Rand (v2) instance that uses source that is seeded with random bytes from crypto.rand.Read().
func Prompt ¶
Prompt prompts on fout for input and then reads a line of text from fin. The line of text is returned with the trailing EOL sequence removed.
In the common case of wanting to read and write from and to os.Stdin and os.Stdout, they should be wrapped to be buffered as follows which should only happen once probably early in your main() function:
fin := bufio.NewReader(os.Stdin) fout := bufio.NewWriter(os.Stdout)
The reason for having to use buffered I/O for stdin and stdout is to avoid conflicts with other parts of the code that might need to use stdin and stdout. You cannot mix buffered I/O and non-buffered I/O without losing some bytes along the way. You cannot even create multiple buffered I/O wrappers around stdin or stdout without losing bytes along the way.
Also see PromptUnbuffered().
func PromptUnbuffered ¶
Prompt prompts on os.Stdout for input and then reads a line of text from os.Stdin. The line of text is returned with the trailing EOL sequence removed.
Also see Prompt().
func ReadLine ¶
ReadLine returns the next line of text with EOL sequence still attached. Call [jrutil.StripEOL()] to remove the EOL sequence. Valid end-of-line sequences are "\r", "\n", or "\r\n". This method assumes the input stream is UTF-8 encoded. Performance should be improved if r is passed in as a bufio.Reader. Also see [ForEachLine()].
func Reduce ¶
Reduce uses the function f to reduce the input slice xs to a single output value. The accumulator is initialized with init. f will be called once for each element in xs with the accumulator as the first argument and the element of xs as the second argument. After calling f for all element of xs, the accumulator is returned.
func SliceToSet ¶
func SliceToSet[T comparable](xs []T) map[T]int
SliceToSet returns the set of elements in the slice where the keys in the map that return for the elements in the set.
func StripEOL ¶
StripEOL returns the line of text with its EOL sequence removed. This function strips Unix ('\n'), DOS ('\r\n'), and Mac ('\r') EOL sequences.
func SubtractSet ¶
func SubtractSet[T comparable](xs, ys map[T]int) map[T]int
SubtractSet returns the set of elements in xs that are not in ys. This is the same as the "relative complement" of ys with respect to xs. The sets can be generated using SliceToSet.
func SubtractSlice ¶
func SubtractSlice[T comparable](xs, ys []T) []T
SubtractSlice returns the slice of elements in xs that are not in ys. This is the same as the "relative complement" of ys with respect to xs.
Types ¶
type Pair ¶
Pair holds a pair of values.
func MapToPairs ¶
func MapToPairs[T1 comparable, T2 any](m map[T1]T2) []Pair[T1, T2]
MapToPairs converts the key/value pairs in the unordered input map m to an unordered slice of key/value pairs. For example, if you want to sort the key/value pairs in a map by value, you can do the following:
// Create the map. counts := map[string]uint64{ "foo": 10, "bar": 3, "baz": 7, } // Convert the map into a list of key/value pairs. kvPairs := jrutil.MapToPairs(counts) // Sort the key/value pairs. sort.Slice(kvPairs, func (i, j int) bool { return kvPairs[i].Second < kvPairs[j].Second }) // Print the results. for _, kvPair := range kvPairs { fmt.Printf("%v: %v\n", kvPair.First, kvPair.Second) }
type SList ¶
type SList[T any] struct { // contains filtered or unexported fields }
SList is a value and a recursive pointer to another list.
func NewSListFromSlice ¶
NewSListFromSlice returns a new SList from the slice xs by performing a shallow copy on each element in xs.
func (*SList[T]) Contains ¶
Contains returns true if the list contains an element that satisfies the predicate. Note that we cannot just pass in an element of type T to compare directly because T is constrained by "any" not "comparable". This is done so SList works with more types.
func (*SList[T]) Drop ¶
Drop removes the first n elements from the front of the list. This method is relatively efficient because it does not have to allocate or deallocate any memory.
func (*SList[T]) DropUntil ¶
DropUntil removes the elements from the head of the list until the predicate is true.
func (*SList[T]) DropWhile ¶
DropWhile removes elements from the head of the list while the predicate is true.
func (*SList[T]) Equal ¶
Equal returns true if the two lists have the same length and if every element in this xs list is equal to the corresponding element in the ys list as determined by the isEqual function.
func (*SList[T]) Merge ¶
Merge the sorted lists xs and ys into a new sorted list and return the result.
func (*SList[T]) PushFront ¶
PushFront appends the value to the front of the list. This method is O(1) is generally used to build the list back-to-front.
func (*SList[T]) Reverse ¶
Reverse returns a new list that is the reverse of this list. This method is often necessary, but it is O(N) and should be used sparingly.
func (*SList[T]) Take ¶
Take returns the first n elements. This method is relatively inefficient because it has to build the new list that is returned. (Profiling shows this is implementation is faster (even for small lists) than a recursive implementation even though this implement requires a call to Reverse().)
func (*SList[T]) TakeUntil ¶
TakeUntil returns elements from the head of the list until the predicate is true. This method is O(N) and should be used sparingly if taking many elements.
type Stack ¶
type Stack[T comparable] struct { // contains filtered or unexported fields }
Stack holds a stack of values implemented as a slice.
func (*Stack[T]) Contains ¶
Contains returns true if the stack contains the item; otherwise, it returns false.
type Vector ¶
Vector is a type alias for []any.
func (Vector[T]) Insert ¶
Insert inserts the value at the index. If the index is greater than the length of the vector, the item is inserted at the end of the vector. This method is O(N) and should be used sparingly.