builders

package
v0.0.0-...-e0320dd Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

builders package provide utilities to generate iterators

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Channel

func Channel[V any](channel chan V) iter.Seq2[int, V]

Channel allows you to convert a channel into an interator

Example
package main

import (
	"fmt"
	"slices"

	"github.com/4strodev/iterago/builders"
	"github.com/4strodev/iterago/transformers"
)

func main() {
	channel := make(chan int)
	go func() {
		for _, v := range builders.Range(0, 10, 1) {
			channel <- v
		}
		close(channel)
	}()

	iterator := builders.Channel(channel)
	evenIterator := transformers.Filter(iterator, func(i int, v int) bool {
		return v%2 == 0
	})
	evenValues := slices.Collect(transformers.Values(evenIterator))
	fmt.Println(evenValues)
}
Output:

[0 2 4 6 8]

func Range

func Range(start int, end int, jump int) iter.Seq2[int, int]

Range returns an iterator with the specified boundaries it starts the iterator with an iteration value that is equal to start and ends when the iteration value is equal to end. After each iteration the next value is calculated summing jump to the current value. If the parameters are put correctly an infinite or reverse iterator can be created.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/4strodev/iterago/builders"
	"github.com/4strodev/iterago/transformers"
)

func main() {
	rangeIterator := builders.Range(0, 10, 2)

	slice := slices.Collect(transformers.Values(rangeIterator))
	fmt.Println(slice)
}
Output:

[0 2 4 6 8]
Example (Infinite)
package main

import (
	"fmt"

	"github.com/4strodev/iterago/builders"
)

func main() {
	// Infinite loop
	for _, v := range builders.Range(0, -1, 1) {
		if v == 10 {
			break
		}
		fmt.Println(v)
	}
}
Output:

0
1
2
3
4
5
6
7
8
9
Example (Reverse)
package main

import (
	"fmt"
	"slices"

	"github.com/4strodev/iterago/builders"
	"github.com/4strodev/iterago/transformers"
)

func main() {
	rangeIterator := builders.Range(10, 0, -2)

	slice := slices.Collect(transformers.Values(rangeIterator))
	fmt.Println(slice)
}
Output:

[10 8 6 4 2]

Types

This section is empty.

Jump to

Keyboard shortcuts

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