iter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

iter

Go Report Card Go Reference CI Code Test Coverage

iter provides tools to simplify working with Iterators(officially introduced in Go 1.23 release).

Example

    package main

    import (
        "fmt"
        "slices"
        "github.com/ameghdadian/iter"
    )

    func main() {
        a := []int{1,2,3}
        b := []int{4,5,6}

        // Combine arbitrary number of slices into an iterator
        it := iter.Concat(a,b)
        for v := range it {
            fmt.Println(v)
        }

        // Combine arbitrary number of iterators into a single iterator
        it1 := slices.Values(a)
        it2 := slices.Values(b)

        combined := iter.ConcatIter(it1, it2)
        for v := range combined {
            fmt.Println(v)
        }

        // ...
    }

License

This is project is licensed under the Apache Version 2.0 License. See LICENSE for details.

Documentation

Overview

Package iter provides utility functions for working with iterators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

func Concat[Slice []T, T any](seqs ...Slice) iter.Seq[T]

Concat receives an arbitrary number of slices and returns an iterator to iterate on them successively, in the order they are given.

func ConcatIter

func ConcatIter[T any](iters ...iter.Seq[T]) iter.Seq[T]

ConcatIter receives an arbitrary number of iterators and returns an iterator to iterate over the elements, in the order they are given.

func Filter

func Filter[T comparable](it iter.Seq[T], filterFn func(T) bool) iter.Seq[T]

Filter returns an iterator that contains the elements of `it` for which filterFn returns true.

func ForEach

func ForEach[T any](it iter.Seq[T], fn func(int, T))

ForEach executes a user-supplied function on each element of `it`.

func Map

func Map[T, R any](it iter.Seq[T], mapFn func(T) R) iter.Seq[R]

Map executes a user-supplied function on each element of the `it` and returns an iterator over modified elements.

func Reduce

func Reduce[T, R any](it iter.Seq[T], reducer func(acc R, cur T) R, init R) R

Reduce executes a user-supplied `reducer` function on each element of the iterator `it`, in order, passing in the return value from the calculation on the preceding element.

func Shuffle

func Shuffle[Slice []T, T any](seq Slice, swapFn func(i, j int)) iter.Seq[T]

Shuffle receives a slice and a swap function. It returns an iterator over the slice shuffled elements.

NOTE: In order not to allocate extra memory, it shuffles given slice(array) in place. If you need the original slice(array) untouched, you can clone your slice using slices.Clone and then pass it as the argument.

Types

This section is empty.

Jump to

Keyboard shortcuts

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