This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Iterator - String helpersโ
This page lists all operations on strings, available in the it
lo sub-package.
ChunkStringโ
Returns a sequence of chunks of length
size
from the input string. If the string length is not a multiple ofsize
, the final chunk contains the remaining characters. Panics ifsize <= 0
.Examples:
// Even split
seq := it.ChunkString("123456", 2)
var out []string
for s := range seq { out = append(out, s) }
// out == []string{"12", "34", "56"}// Remainder chunk
seq := it.ChunkString("1234567", 2)
var out []string
for s := range seq { out = append(out, s) }
// out == []string{"12", "34", "56", "7"}// Empty and small inputs
seq1 := it.ChunkString("", 2)
seq2 := it.ChunkString("1", 2)
// seq1 yields ""
// seq2 yields "1"Similar:Prototype:func ChunkString[T ~string](str T, size int) iter.Seq[T]
CutPrefixโ
CutPrefix returns collection without the provided leading prefix and reports whether it found the prefix.
If collection doesn't start with prefix, CutPrefix returns collection, false.
If prefix is empty, CutPrefix returns collection, true.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
after, found := it.CutPrefix(collection, []int{1, 2})
var result []int
for item := range after {
result = append(result, item)
}
// result contains [3, 4], found is true
after2, found2 := it.CutPrefix(collection, []int{9, 10})
var result2 []int
for item := range after2 {
result2 = append(result2, item)
}
// result2 contains [1, 2, 3, 4], found2 is falseSimilar:Prototype:func CutPrefix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (after I, found bool)
CutSuffixโ
CutSuffix returns collection without the provided ending suffix and reports whether it found the suffix.
If collection doesn't end with suffix, CutSuffix returns collection, false.
If suffix is empty, CutSuffix returns collection, true.collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
}
before, found := it.CutSuffix(collection, []int{3, 4})
var result []int
for item := range before {
result = append(result, item)
}
// result contains [1, 2], found is trueSimilar:Prototype:func CutSuffix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (before I, found bool)
Trimโ
Trim removes all the leading and trailing cutset from the collection.
collection := func(yield func(int) bool) {
yield(0)
yield(0)
yield(1)
yield(2)
yield(3)
yield(0)
yield(0)
}
trimmed := it.Trim(collection, 0)
var result []int
for item := range trimmed {
result = append(result, item)
}
// result contains [1, 2, 3]Similar:Prototype:func Trim[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I
TrimFirstโ
TrimFirst removes all the leading cutset from the collection.
collection := func(yield func(int) bool) {
yield(0)
yield(0)
yield(1)
yield(2)
yield(3)
}
trimmed := it.TrimFirst(collection, 0)
var result []int
for item := range trimmed {
result = append(result, item)
}
// result contains [1, 2, 3]Prototype:func TrimFirst[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I
TrimLastโ
TrimLast removes all the trailing cutset from the collection.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(0)
yield(0)
}
trimmed := it.TrimLast(collection, 0)
var result []int
for item := range trimmed {
result = append(result, item)
}
// result contains [1, 2, 3]Prototype:func TrimLast[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I
TrimPrefixโ
TrimPrefix removes all the leading prefix from the collection.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(1)
yield(2)
yield(3)
}
trimmed := it.TrimPrefix(collection, []int{1, 2})
var result []int
for item := range trimmed {
result = append(result, item)
}
// result contains [1, 2, 3]Similar:Prototype:func TrimPrefix[T comparable, I ~func(func(T) bool)](collection I, prefix []T) I
TrimSuffixโ
TrimSuffix removes all the trailing suffix from the collection.
collection := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
yield(4)
yield(3)
yield(4)
}
trimmed := it.TrimSuffix(collection, []int{3, 4})
var result []int
for item := range trimmed {
result = append(result, item)
}
// result contains [1, 2]Similar:Prototype:func TrimSuffix[T comparable, I ~func(func(T) bool)](collection I, suffix []T) I