itertools

package module
v0.1.2 Latest Latest
Warning

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

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

README

itertools

Go Report Card Go Reference CI Code Test Coverage

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

Example

    package main

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

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

        // Combine arbitrary number of slices into an iterator
        it := itertools.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 := itertools.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 itertools provides utility functions to work 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](seqs ...iter.Seq[T]) iter.Seq[T]

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

func Filter

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

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

func ForEach

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

ForEach executes a user-supplied function on each element of the sequence. It does not return any output.

func Map

func Map[Slice []T, T any](seq Slice, mapFn func(int, T) T) iter.Seq2[int, T]

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

func Reduce

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

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

func Reverse

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

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

For example, given the input:

Reverse([]int{10, 20, 30}, []int{40, 50, 60})

The iterator following the output:

{60, 50, 40, 30, 20, 10}

func Shuffle

func Shuffle[Slice []T, T any](seq Slice) iter.Seq[T]

Shuffle receives a slice and returns an iterator over its 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