Skip to main content

Type manipulation - Iterator helpers

This page lists all type operations available in the it lo sub-package.

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!

  • Returns an empty sequence of the specified type.

    Examples:

    emptySeq := it.Empty[int]()
    count := 0
    for range emptySeq {
    count++
    }
    // count == 0
    emptySeq := it.Empty[string]()
    var result []string
    for v := range emptySeq {
    result = append(result, v)
    }
    // result is empty slice
    Prototype:
    func Empty[T any]() iter.Seq[T]
  • Returns true if the sequence is empty. Will consume the entire sequence to check.

    Examples:

    seq := func(yield func(int) bool) {
    // empty sequence
    }
    empty := it.IsEmpty(seq)
    // empty == true
    seq := func(yield func(int) bool) {
    _ = yield(1)
    _ = yield(2)
    }
    empty := it.IsEmpty(seq)
    // empty == false
    Prototype:
    func IsEmpty[T any](collection iter.Seq[T]) bool
  • Returns the first non-empty sequence from the provided arguments, with a boolean indicating if a non-empty sequence was found.

    emptySeq := func(yield func(int) bool) bool {
    return false // empty sequence
    }
    nonEmptySeq := it.Range(3)
    result, ok := it.CoalesceSeq(emptySeq, nonEmptySeq, emptySeq)
    // iter.Seq[int] yielding 0, 1, 2, true

    emptyStrSeq := func(yield func(string) bool) bool {
    return false // empty sequence
    }
    strSeq := func(yield func(string) bool) bool {
    yield("a")
    yield("b")
    return true
    }
    result, ok = it.CoalesceSeq(emptyStrSeq, strSeq)
    // iter.Seq[string] yielding "a", "b", true

    result, ok = it.CoalesceSeq(emptySeq, emptyStrSeq)
    // nil sequence, false
    Prototype:
    func CoalesceSeq[T any](v ...iter.Seq[T]) (iter.Seq[T], bool)
  • Returns the first non-empty sequence from the provided arguments, or an empty sequence if all arguments are empty.

    emptySeq := func(yield func(int) bool) bool {
    return false // empty sequence
    }
    nonEmptySeq := it.Range(3)
    result := it.CoalesceSeqOrEmpty(emptySeq, nonEmptySeq, emptySeq)
    // iter.Seq[int] yielding 0, 1, 2

    emptyStrSeq := func(yield func(string) bool) bool {
    return false // empty sequence
    }
    strSeq := func(yield func(string) bool) bool {
    yield("a")
    yield("b")
    return true
    }
    result = it.CoalesceSeqOrEmpty(emptyStrSeq, strSeq)
    // iter.Seq[string] yielding "a", "b"

    result = it.CoalesceSeqOrEmpty(emptySeq, emptyStrSeq)
    // empty sequence (yields nothing)
    Prototype:
    func CoalesceSeqOrEmpty[T any](v ...iter.Seq[T]) iter.Seq[T]
  • ToSeqPtr returns a sequence of pointers to each value.

    collection := func(yield func(int) bool) {
    yield(1)
    yield(2)
    yield(3)
    }

    ptrs := it.ToSeqPtr(collection)
    var result []*int
    for ptr := range ptrs {
    result = append(result, ptr)
    }
    // result contains pointers to 1, 2, 3
    Prototype:
    func ToSeqPtr[T any](collection iter.Seq[T]) iter.Seq[*T]
  • FromSeqPtr returns a sequence with the pointer values.
    Returns a zero value in case of a nil pointer element.

    one := 1
    two := 2
    var three *int = nil

    collection := func(yield func(*int) bool) {
    yield(&one)
    yield(&two)
    yield(three)
    }

    values := it.FromSeqPtr(collection)
    var result []int
    for val := range values {
    result = append(result, val)
    }
    // result contains [1, 2, 0]
    Variant:
    Prototype:
    func FromSeqPtr[T any](collection iter.Seq[*T]) iter.Seq[T]
  • FromSeqPtrOr returns a sequence with the pointer values or the fallback value.

    one := 1
    var two *int = nil

    collection := func(yield func(*int) bool) {
    yield(&one)
    yield(two)
    }

    values := it.FromSeqPtrOr(collection, 99)
    var result []int
    for val := range values {
    result = append(result, val)
    }
    // result contains [1, 99]
    Variant:
    Prototype:
    func FromSeqPtrOr[T any](collection iter.Seq[*T], fallback T) iter.Seq[T]
  • ToAnySeq returns a sequence with all elements mapped to any type.

    collection := func(yield func(int) bool) {
    yield(1)
    yield(2)
    yield(3)
    }

    anySeq := it.ToAnySeq(collection)
    var result []any
    for item := range anySeq {
    result = append(result, item)
    }
    // result contains [1, 2, 3] as any type
    Variant:
    Prototype:
    func ToAnySeq[T any](collection iter.Seq[T]) iter.Seq[any]
  • FromAnySeq returns a sequence with all elements mapped to a type.
    Panics on type conversion failure.

    collection := func(yield func(any) bool) {
    yield(1)
    yield(2)
    yield("three") // This will cause panic
    }

    intSeq := it.FromAnySeq[int](collection)
    // This will panic when trying to convert "three" to int
    Variant:
    Prototype:
    func FromAnySeq[T any](collection iter.Seq[any]) iter.Seq[T]
  • Empty returns an empty sequence.

    empty := it.Empty[int]()
    var result []int
    for item := range empty {
    result = append(result, item)
    }
    // result is empty []

    IsEmpty returns true if the sequence is empty.

    emptySeq := it.Empty[int]()
    notEmptySeq := func(yield func(int) bool) {
    yield(1)
    }

    fmt.Println(it.IsEmpty(emptySeq)) // true
    fmt.Println(it.IsEmpty(notEmptySeq)) // false

    IsNotEmpty returns true if the sequence is not empty.

    emptySeq := it.Empty[int]()
    notEmptySeq := func(yield func(int) bool) {
    yield(1)
    }

    fmt.Println(it.IsNotEmpty(emptySeq)) // false
    fmt.Println(it.IsNotEmpty(notEmptySeq)) // true

    CoalesceSeq returns the first non-empty sequence.

    empty1 := it.Empty[int]()
    empty2 := it.Empty[int]()
    notEmpty := func(yield func(int) bool) {
    yield(1)
    yield(2)
    }

    result, found := it.CoalesceSeq(empty1, empty2, notEmpty)
    // found is true, result is the notEmpty sequence

    result2, found2 := it.CoalesceSeq(empty1, empty2)
    // found2 is false, result2 is an empty sequence

    CoalesceSeqOrEmpty returns the first non-empty sequence.

    empty1 := it.Empty[int]()
    empty2 := it.Empty[int]()
    notEmpty := func(yield func(int) bool) {
    yield(1)
    yield(2)
    }

    result := it.CoalesceSeqOrEmpty(empty1, empty2, notEmpty)
    // result is the notEmpty sequence

    result2 := it.CoalesceSeqOrEmpty(empty1, empty2)
    // result2 is an empty sequence
    Prototypes:
    func Empty[T any]() iter.Seq[T]
    func IsEmpty[T any](collection iter.Seq[T]) bool
    func IsNotEmpty[T any](collection iter.Seq[T]) bool
    func CoalesceSeq[T any](v ...iter.Seq[T]) (iter.Seq[T], bool)
    func CoalesceSeqOrEmpty[T any](v ...iter.Seq[T]) iter.Seq[T]