iter

package
v2.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[V any](iter Iterator[V]) []V

Collect consumes an iterator and returns a slice of all items yielded.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	fmt.Println(iter.Collect(iter.Lift([]int{1, 2})))
}
Output:

[1 2]
Example (Method)
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	fmt.Println(iter.Lift([]int{1, 2}).Collect())
}
Output:

[1 2]

Types

type Iterator

type Iterator[V any] iter.Seq[V]

Iterator is a wrapper around the iter.Seq type that allowed for method chaining of iterators found in this package.

func Count

func Count() Iterator[int]

Count yields an infinite sequence of integers, starting from 0.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	for i := range iter.Count().Take(3) {
		fmt.Println(i)
	}

}
Output:

0
1
2

func Lift

func Lift[V any](slice []V) Iterator[V]

Lift yields all items in the provided slice.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	for i := range iter.Lift([]int{1, 2}) {
		fmt.Println(i)
	}

}
Output:

1
2

func Take

func Take[V any](delegate Iterator[V], limit int) Iterator[V]

Take limits the number of elements yielded by a delegate iterator to a maximum limit.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	for i := range iter.Take(iter.Lift([]int{1, 2, 3}), 2) {
		fmt.Println(i)
	}

}
Output:

1
2
Example (Method)
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/v2/iter"
)

func main() {
	for i := range iter.Lift([]int{1, 2, 3}).Take(2) {
		fmt.Println(i)
	}

}
Output:

1
2

func (Iterator[V]) Collect

func (iter Iterator[V]) Collect() []V

Collect is a convenience method for chaining Collect after an iterator.

func (Iterator[V]) Take

func (iter Iterator[V]) Take(limit int) Iterator[V]

Take is a convenience method for chaining Take after an iterator.

Jump to

Keyboard shortcuts

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