package
0.305.0
Repository: https://github.com/adamluzsi/frameless.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

iterkit

Go Reference

iterkit is a Go package that streamlines working with data sequences, prioritising an intuitive developer experience. The package's helper functions are designed with streaming in mind, ensuring mindful memory usage. It offers various iterator implementations and utilities that enable developers to process, transform, and manage data efficiently.

Installation

To integrate iterkit into your Go project, run:

go get go.llib.dev/frameless/pkg/iterkit

Working with Failable Iterators

iterkit aims to make it easier to work with iterators that interact with external resources that can fail. These are represented by the iterkit.SeqE[T] type, which is an alias for iter.Seq2[T, error]. This represents an iterator sequence that may potentially fail at any time. The name draws inspiration from the standard library's Seq2, but with a key distinction – the suffix "E" highlights the possibility of errors occurring during iteration.

type SeqE[T any] = iter.Seq2[T, error]

For more information on how we chose this approach, see our detailed explanation.

Features

Stream Operations:

iter.Seqiter.Seq2iterkit.SeqE
Collect the values of an iteratorCollectCollect2,
Collect2Map,
Collect2KV
CollectE
Convert slice to iteratorSlice1SliceE
Create an empty iteratorEmptyEmpty2EmptyE
Transform values between typesMapMap2MapE
Collect all valuesCollectCollect2,
Collect2KV,
Collect2Map
CollectE
Filter unwanted values out from a data sequenceFilterFilter2Filter
Merge multiple data sequence into oneMergeMerge2MergeE
Turn the data stream into a chunked/batch data streamBatchBatchE
Make an data stream only consumable onceOnceOnce2OnceE
Limit number of itemsLimit,
Head
Limit2,
Head2
LimitE,
HeadE
Take N valuesTake,
TakeAll
Take2,
TakeAll2
TakeE,
TakeAllE
Count all elementsCountCount2CountE
Enable FanOut/FanIn with iteratorsSyncSync2SyncE
Create iterator from paginated data sourceFromPages
Reduce a data sequence into a result valueReduce,
ReduceErr
Reduce2ReduceE,
ReduceEErr
,Reduce,
ReduceErr
Consume an iterator to count its elementsCountCount2CountE
Limit the maximum returned element countLimitLimit2LimitE
Offset what will be the first element in a sequenceOffsetOffset2OffsetE
Get the first element from a data sequenceFirstFirst2FirstE
Get the last element from a data sequenceLastLast2LastE
Work with a SeqE[T] like it is simple Seq[T]OnSeqEValue

Constructors:

iterkit.SeqE
Create failable data sequence in a clean and idiomatic wayFromSeqE[T]
Turn a bufio scanner's stream into a data sequenceBufioScannerSingleUseSeqE[T]
Create int range between a given boundaryIntRange,
IntRangeE
Seq[int],
SeqE[int]
Create a character range between a given boundaryCharRange,
CharRangeE
Seq[rune],
SeqE[rune]
Turn a Channel into a data sequenceChan,
ChanE
Seq[T],
SeqE[T]
Create sequence that represent a persistent errorError,
ErrorF
SeqE[T]
Express a single value as a data sequenceOf,
Of2,
OfE
Seq[T],
Seq2[K, V],
SeqE[T]
Cast a sequence into a SeqEToSeqESeqE[T]
Split a SeqE into a Seq and a closer funcSplitSeqESeq[T] + func() error
Express a data sequence as a channel valueToChanchan T
Create a stdlib iterator out from a stateful OOP pull iteratorFromPullIterSeqE[T]

Pull Operations:

iter.Seqiter.Seq2iterkit.SeqE
Take the first N element from a pull iteratorTakeTake2TakeE
Take all the elements from a pull iteratorTakeAllTakeAll2TakeAllE
Collect all elements from a pull iterator and close itCollectPullCollectEPull
Convert back a pull iterator into a single use SequenceFromPullFromPull2FromPullE

Usage

Below is an example demonstrating the use of iterkit to filter and transform a slice:

package main

import (
	"fmt"
	"slices"

	"go.llib.dev/frameless/pkg/iterkit"
)

func main() {
	// Create an iterator from a slice
	numbers := slices.Values([]int{1, 2, 3, 4, 5, 6})

	// Filter even numbers
	evens := iterkit.Filter(numbers, func(n int) bool {
		return n%2 == 0
	})

	// Square each even number
	squares := iterkit.Map(evens, func(n int) int {
		return n * n
	})

	// Collect results into a slice
	result := iterkit.Collect(squares)

	fmt.Println(result) // Output: [4 16 36]
}