it

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 2 Imported by: 0

README

it

latest go coverage Go Report Card

💥 it is a Go module that provides a variety of iterator building blocks, based on the Go 1.23+ iterators.

It is entirely written in the Go standard library, making it lightweight and dependency-free.

It is inspired by the Rust std::iter::Iterator trait.

Getting Started

Installation
go get github.com/kyminbb/it
Usage
import "github.com/kyminbb/it"

See https://pkg.go.dev/github.com/kyminbb/it for the detailed usage doc and examples.

Contributing

Prerequisites
Getting Started
  • Fix open issues or request new features
  • Fork this repository to make changes
    • Run just test to run the tests

License

See LICENSE (MIT).

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.1.0

func All[V any](s []V) iter.Seq[V]

All returns an iterator over all elements of s.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	for v := range nums {
		fmt.Println(v)
	}
}
Output:

1
2
3

func Chain added in v0.1.0

func Chain[V any](seqs ...iter.Seq[V]) iter.Seq[V]

Chain returns an iterator that yields elements from each of seqs in turn.

If seqs is empty, the returned iterator is also empty.

Example
package main

import (
	"fmt"
	"iter"

	"github.com/kyminbb/it"
)

func main() {
	seqs := []iter.Seq[int]{it.All([]int{1, 2}), it.All([]int{3, 4})}
	chain := it.Chain(seqs...)
	for v := range chain {
		fmt.Println(v)
	}
}
Output:

1
2
3
4

func Count added in v0.1.0

func Count[V any](seq iter.Seq[V]) int

Count iterates over seq and returns the number of elements.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	fmt.Println(it.Count(nums))
}
Output:

3

func Cycle added in v0.1.0

func Cycle[V any](seq iter.Seq[V]) iter.Seq[V]

Cycle returns an iterator that repeats seq endlessly.

If seq is empty, the returned iterator is also empty.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	cycle := it.Cycle(nums)
	i := 0
	for v := range cycle {
		if i == 6 {
			break
		}
		fmt.Println(v)
		i++
	}
}
Output:

1
2
3
1
2
3

func Empty added in v0.1.0

func Empty[V any]() iter.Seq[V]

Empty returns an iterator that yields nothing.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	empty := it.Empty[int]()
	for range empty {
		fmt.Println("This should not be printed")
	}
}

func Filter added in v0.1.0

func Filter[V any](seq iter.Seq[V], predicate func(V) bool) iter.Seq[V]

Filter returns an iterator that yields only the elements of seq for which predicate is true.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, -2, 3})
	positives := it.Filter(nums, func(x int) bool { return x > 0 })
	for v := range positives {
		fmt.Println(v)
	}
}
Output:

1
3

func Fold added in v0.1.0

func Fold[V, R any](seq iter.Seq[V], init R, f func(R, V) R) R

Fold applies f to each element of seq and returns the accumulated result.

init is the initial value of the accumulator. f takes the accumulator and an element of seq as arguments. The result of each call to f becomes the accumulator for the next call.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	sum := it.Fold(nums, 0, func(acc, v int) int { return acc + v })
	fmt.Println(sum)

	nums = it.All([]int{3, 4, 5})
	product := it.Fold(nums, 1, func(acc, v int) int { return acc * v })
	fmt.Println(product)
}
Output:

6
60

func Last added in v0.1.0

func Last[V any](seq iter.Seq[V]) (V, bool)

Last iterates over seq and returns the last element. The second return value reports whether the element exists (seq is not empty).

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	fmt.Println(it.Last(nums))

	nums = it.All([]int{})
	fmt.Println(it.Last(nums))
}
Output:

3 true
0 false

func Map added in v0.1.0

func Map[V, R any](seq iter.Seq[V], f func(V) R) iter.Seq[R]

Map returns an iterator that calls f on each element of seq.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	doubles := it.Map(nums, func(x int) int { return x * 2 })
	for v := range doubles {
		fmt.Println(v)
	}
}
Output:

2
4
6

func Max added in v0.1.0

func Max[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

Max returns the maximum element of seq. The second return value reports whether the element exists (seq is not empty).

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	fmt.Println(it.Max(nums))

	nums = it.All([]int{})
	fmt.Println(it.Max(nums))
}
Output:

3 true
0 false

func Min added in v0.1.0

func Min[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

Min returns the minimum element of seq. The second return value reports whether the element exists (seq is not empty).

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{3, 2, 1})
	fmt.Println(it.Min(nums))

	nums = it.All([]int{})
	fmt.Println(it.Min(nums))
}
Output:

1 true
0 false

func Nth added in v0.1.0

func Nth[V any](seq iter.Seq[V], n int) (V, bool)

Nth returns the nth element of seq. The second return value reports whether the element exists.

The count is zero-based, so Nth(seq, 0) returns the first element.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	fmt.Println(it.Nth(nums, 1))
	fmt.Println(it.Nth(nums, 3))
}
Output:

2 true
0 false

func Repeat added in v0.1.0

func Repeat[V any](v V) iter.Seq[V]

Repeat returns an iterator that yields v endlessly.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	repeat := it.Repeat(3)
	i := 0
	for v := range repeat {
		if i == 3 {
			break
		}
		fmt.Println(v)
		i++
	}
}
Output:

3
3
3

func Skip added in v0.1.0

func Skip[V any](seq iter.Seq[V], n int) iter.Seq[V]

Skip returns an iterator that skips the first n elements of seq and yields the rest.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	skip := it.Skip(nums, 2)
	for v := range skip {
		fmt.Println(v)
	}
}
Output:

3

func Take added in v0.1.0

func Take[V any](seq iter.Seq[V], n int) iter.Seq[V]

Take returns an iterator that yields the first n elements of seq, or fewer if seq ends sooner.

Example
package main

import (
	"fmt"

	"github.com/kyminbb/it"
)

func main() {
	nums := it.All([]int{1, 2, 3})
	take := it.Take(nums, 2)
	for v := range take {
		fmt.Println(v)
	}
}
Output:

1
2

Types

This section is empty.

Jump to

Keyboard shortcuts

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