Skip to main content
Help improve this documentation

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.

  • Returns a sequence of chunks of length size from the input string. If the string length is not a multiple of size, the final chunk contains the remaining characters. Panics if size <= 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"
    Prototype:
    func ChunkString[T ~string](str T, size int) iter.Seq[T]
  • 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 false
    Prototype:
    func CutPrefix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (after I, found bool)
  • 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 true
    Prototype:
    func CutSuffix[T comparable, I ~func(func(T) bool)](collection I, separator []T) (before I, found bool)
  • 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]
    Prototype:
    func Trim[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I
  • 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]
    Variant:
    Prototype:
    func TrimFirst[T comparable, I ~func(func(T) bool)](collection I, cutset ...T) I
  • 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 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]
    Prototype:
    func TrimPrefix[T comparable, I ~func(func(T) bool)](collection I, prefix []T) I
  • 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]
    Prototype:
    func TrimSuffix[T comparable, I ~func(func(T) bool)](collection I, suffix []T) I