Documentation ¶
Index ¶
- Variables
- func Accumulate[T constraints.Ordered](iter iter.Iterable[T]) iter.Iterable[T]
- func All[T any](iter iter.Iterable[T], function predicates.Predicate[T]) bool
- func Any[T any](iter iter.Iterable[T], function predicates.Predicate[T]) bool
- func Asc[T Number](this, that T) T
- func Async[T any, V any](ctx context.Context, inIter iter.Iterable[T], workers int, ...) iter.Iterable[V]
- func At[T any](iter iter.Iterable[T], index int) (T, bool)
- func Batch[T any](iter iter.Iterable[T], size int) iter.Iterable[iter.Iterable[T]]
- func Chain[T any](chain ...iter.Iterable[T]) iter.Iterable[T]
- func Count[T any](iter iter.Iterable[T]) int
- func CountIf[T any](iter iter.Iterable[T], function predicates.Predicate[T]) int
- func Cycle[T any](iter iter.Iterable[T]) iter.Iterable[T]
- func Decrementer(start int) iter.Iterable[int]
- func Desc[T Number](this, that T) T
- func Drain[T any](iter iter.Iterable[T])
- func Drop[T any](iter iter.Iterable[T], count int) iter.Iterable[T]
- func Each[T any](iter iter.Iterable[T], function func(item T))
- func EachUntil[T any](iter iter.Iterable[T], function func(item T) bool)
- func Empty[T any]() iter.Iterable[T]
- func EndsWith[T comparable](items, suffix iter.Iterable[T]) bool
- func Entries[K comparable, V any](items map[K]V) iter.Iterable[tuple.Tuple[K, V]]
- func Enumerate[T any](items iter.Iterable[T]) iter.Iterable[tuple.Tuple[int, T]]
- func Equals[T comparable](a, b iter.Iterable[T]) bool
- func Filter[T any](iter iter.Iterable[T], fn predicates.Predicate[T]) iter.Iterable[T]
- func Find[T any](iter iter.Iterable[T], function predicates.Predicate[T]) (T, bool)
- func Flatten[T any](chain ...iter.Iterable[iter.Iterable[T]]) iter.Iterable[T]
- func Incrementer(start int) iter.Iterable[int]
- func Intersection[K comparable](first, second iter.Iterable[K]) iter.Iterable[K]
- func Join[T any](iter iter.Iterable[T], separator T) iter.Iterable[T]
- func Keys[K comparable, V any](items map[K]V) iter.Iterable[K]
- func Last[T any](iter iter.Iterable[T]) (T, bool)
- func LastIf[T any](iter iter.Iterable[T], function predicates.Predicate[T]) (T, bool)
- func Lines(src io.Reader) iter.Iterable[string]
- func Log[T any](iter iter.Iterable[T], logger Logger, format func(item T) string) iter.Iterable[T]
- func Map[T any, V any](iter iter.Iterable[T], function MapFn[T, V]) iter.Iterable[V]
- func Max[T constraints.Ordered](iter iter.Iterable[T]) T
- func Min[T constraints.Ordered](iter iter.Iterable[T]) T
- func Mix[T any](mix ...iter.Iterable[T]) iter.Iterable[T]
- func Pair[T any](items iter.Iterable[T]) iter.Iterable[tuple.Tuple[T, T]]
- func Partition[T any](input iter.Iterable[T], function predicates.Predicate[T]) (iter.Iterable[T], iter.Iterable[T])
- func Print[T any](iter iter.Iterable[T])
- func Println[T any](iter iter.Iterable[T])
- func Product[T Number](iter iter.Iterable[T]) T
- func Range(start, stop int) iter.Iterable[int]
- func RangeInc(start, stop, inc int) iter.Iterable[int]
- func Reader(iter iter.Iterable[[]byte]) io.Reader
- func Repeat[T any](value T) iter.Iterable[T]
- func Reverse[T any](iter iter.Iterable[T]) iter.Iterable[T]
- func Runes(value string) iter.Iterable[rune]
- func Set[K comparable](items iter.Iterable[K]) map[K]bool
- func Slice[T any](items iter.Iterable[T]) []T
- func SliceN[T any](items iter.Iterable[T], start, stop int) []T
- func Sort[T any](iter iter.Iterable[T], cmp Compare[T]) iter.Iterable[T]
- func StartsWith[T comparable](iter, prefix iter.Iterable[T]) bool
- func Subtract[K comparable](first, second iter.Iterable[K]) iter.Iterable[K]
- func Sum[T constraints.Ordered](iter iter.Iterable[T]) T
- func Tail[T any](input iter.Iterable[T], amount int) iter.Iterable[T]
- func Take[T any](iter iter.Iterable[T], amount int) iter.Iterable[T]
- func Tee[T any](iter iter.Iterable[T]) (iter.Iterable[T], iter.Iterable[T])
- func Union[K comparable](first, second iter.Iterable[K]) iter.Iterable[K]
- func Unique[K comparable](input iter.Iterable[K]) iter.Iterable[K]
- func Values[K comparable, V any](items map[K]V) iter.Iterable[V]
- func Zip[T any, V any](first iter.Iterable[T], second iter.Iterable[V]) iter.Iterable[tuple.Tuple[T, V]]
- type AsyncMapFn
- type Compare
- type IterWriteCloser
- type Logger
- type MapFn
- type Number
Examples ¶
- Accumulate
- All (False)
- All (True)
- Any (False)
- Any (True)
- Async
- At
- Batch
- Chain
- Count
- CountIf
- Cycle
- Drain
- Drop
- Each
- EachUntil
- Entries
- Enumerate
- Equals
- Filter
- Find
- Flatten
- Incrementer
- Intersection
- Join
- Keys
- Last
- Lines
- Log
- Map
- Max
- Min
- Mix
- Partition
- Println
- Product
- Range
- RangeInc
- RangeInc (Reverse)
- Reader
- Repeat
- Reverse
- Runes
- Set
- Slice
- SliceN
- Sort
- Sort (Ascending)
- Sort (Descending)
- StartsWith
- Subtract
- Sum
- Tail
- Take
- Tee
- Union
- Unique
- Values
- Writer
- Zip
Constants ¶
This section is empty.
Variables ¶
var ErrClosedIter = errors.New("io: write on closed iter")
Functions ¶
func Accumulate ¶
Accumulate returns an iterable that yields accumulated values from the input iterable. The accumulated value at each position is the sum of all previous values in the input iterable. The input iterable must contain elements of a type that satisfies the constraints.Ordered interface.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Accumulate(iter.New("a", "b", "c")) itertools.Print(items) }
Output: a, ab, abc
func All ¶
All returns true if all elements in the iterable satisfy the given predicate function. It iterates over the elements of the iterable until it finds an element that does not satisfy the predicate, in which case it returns false. If all elements satisfy the predicate or the iterable is empty, it returns true. The function takes an iterable and a predicate function as arguments. The predicate function should take an element of the iterable as input and return a boolean value indicating whether the element satisfies the condition.
Example (False) ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.All(iter.New(1, 2, 3), func(n int) bool { return n == 1 }) fmt.Println(result) }
Output: false
Example (True) ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.All(iter.New(1, 2, 3), func(n int) bool { return n != 0 }) fmt.Println(result) }
Output: true
func Any ¶
Any returns true if at least one element in the iterable satisfies the given predicate function. It iterates over the elements of the iterable until it finds an element that satisfies the predicate, and then it returns true. If no such element is found, it returns false. The predicate function takes an element of type T as input and returns a boolean value. The iterable must implement the Iterable interface, and the predicate function must implement the Predicate interface.
Example (False) ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.All(iter.New(1, 2, 3), func(n int) bool { return n == 0 }) fmt.Println(result) }
Output: false
Example (True) ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Any(iter.New(1, 2, 3), func(n int) bool { return n == 1 }) fmt.Println(result) }
Output: true
func Asc ¶
func Asc[T Number](this, that T) T
Asc returns the difference between `a` and `b`. It is used to define the ascending order for sorting.
func Async ¶
func Async[T any, V any]( ctx context.Context, inIter iter.Iterable[T], workers int, function AsyncMapFn[T, V], ) iter.Iterable[V]
Async is a function that takes a context, an input iterable, the number of workers, and an async map function. It returns an iterable of the mapped values. The function distributes the work across multiple goroutines, allowing for concurrent processing. The input iterable is divided into chunks and each chunk is processed by a worker goroutine. The number of worker goroutines is determined by the `workers` parameter. The `fn` parameter is the async map function that takes an input value and returns a mapped value. The function returns an iterable of the mapped values, which can be consumed using the `Next` method. If the `workers` parameter is less than 1, an empty iterable is returned.
Example ¶
package main import ( "context" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Async(context.Background(), iter.New(1, 2, 3), 2, func(ctx context.Context, x int) int { return x * 2 }) itertools.Println(items) }
Output: 2 4 6
func At ¶
At returns the element at the specified index in the given iterable. It returns the element and a boolean value indicating whether the element was found. If the index is out of range or negative, it returns the zero value of the element type and false.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result, _ := itertools.At(iter.New(1, 2, 3), 1) fmt.Println(result) }
Output: 2
func Batch ¶
Batch returns an iterable that groups elements from the input iterable into batches of the specified size. Each batch is itself an iterable of elements from the input iterable. The last batch may contain fewer elements if the input iterable's length is not divisible by the batch size.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Batch(iter.New(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 3) itertools.Each(result, func(batch iter.Iterable[int]) { itertools.Print(batch) }) }
Output: 1, 2, 3 4, 5, 6 7, 8, 9 10
func Chain ¶
Chain returns an iterable that combines multiple iterables into a single iterable. The elements from each iterable are returned in the order they appear. The type parameter T represents the type of elements in the iterables.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Chain( iter.New(1, 2, 3), iter.New(4, 5, 6), iter.New(7, 8, 9), ) itertools.Print(items) }
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
func Count ¶
Count returns the total number of elements in the given iterable. It iterates over the iterable until there are no more elements and counts each element encountered.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Count(iter.New("a", "b", "c")) fmt.Println(result) }
Output: 3
func CountIf ¶
CountIf counts the number of elements in the iterable that satisfy the given predicate function. It iterates over the elements of the iterable and applies the predicate function to each element. If the predicate function returns true for an element, it increments the count. Finally, it returns the total count of elements that satisfy the predicate.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { result := itertools.CountIf(iter.New(1, 2, 3, 4, 5), predicates.Even[int]) fmt.Println(result) }
Output: 2
func Cycle ¶
Cycle returns an iterable that cycles through the elements of the input iterable indefinitely. The input iterable must support iteration. The returned iterable will keep track of the elements it has already iterated through, allowing it to cycle through the elements without consuming additional memory.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Cycle(iter.New("a", "b", "c")) itertools.Print(itertools.Take(items, 7)) }
Output: a, b, c, a, b, c, a
func Decrementer ¶
Decrementer returns an iterable that generates a sequence of integers in descending order. The sequence starts from the given start value and decrements by 1 for each subsequent value.
func Desc ¶
func Desc[T Number](this, that T) T
Desc returns the difference between the second argument and the first argument. It is used as a comparison function for sorting in descending order.
func Drain ¶
Drain consumes all elements from the given iterable until it is exhausted. It discards the values returned by the iterable's Next method.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := iter.New("a", "b", "c") itertools.Drain(items) itertools.Print(items) }
Output:
func Drop ¶
Drop returns an iterable that skips the first `count` elements from the input iterable. The returned iterable will yield the remaining elements from the input iterable. If `count` is greater than the length of the input iterable, an empty iterable is returned.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Drop(iter.New("a", "b", "c", "d", "e"), 2) itertools.Print(items) }
Output: c, d, e
func Each ¶
Each applies a function to each item in the iterable. It iterates over the iterable and calls the provided function for each item. The function should take an item of type T as its argument. The iterable should implement the `iter.Iterable` interface. The function does not return any value.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { itertools.Each(iter.New("a", "b", "c"), func(item string) { fmt.Println(item) }) }
Output: a b c
func EachUntil ¶
EachUntil applies the given function to each item in the iterable until the function returns true. It stops iterating and returns immediately when the function returns true. The function takes an item of type T as input and returns a boolean value. If the iterable is empty, the function does nothing.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { itertools.EachUntil(iter.New(1, 2, 3, 4, 5), func(item int) bool { fmt.Println(item) return item == 3 }) }
Output: 1 2 3
func EndsWith ¶
func EndsWith[T comparable](items, suffix iter.Iterable[T]) bool
EndsWith checks if the given `items` ends with the specified `suffix`. It returns `true` if `items` ends with `suffix`, and `false` otherwise. The `items` parameter is an iterable of type `T`, and the `suffix` parameter is also an iterable of type `T`. The `T` type must be comparable.
func Entries ¶
Entries returns an iterable of tuples containing the key-value pairs from the given map. The keys and values can be of any type, as long as the keys are comparable. The returned iterable can be used to iterate over the key-value pairs in a consistent order.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { input := map[string]int{ "apple": 5, "banana": 3, "orange": 2, } items := itertools.Entries(input) itertools.Println(items) }
Output: {apple 5} {banana 3} {orange 2}
func Enumerate ¶
Enumerate returns an iterable that pairs each element of the input iterable with its corresponding index. The index starts from 0.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := iter.New("hello", "world", "!") itertools.Print(itertools.Enumerate(items)) }
Output: {0 hello}, {1 world}, {2 !}
func Equals ¶
func Equals[T comparable](a, b iter.Iterable[T]) bool
Equals checks if two iterables are equal by comparing their elements. It returns true if the iterables have the same elements in the same order, and false otherwise.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { a := iter.New(1, 2, 3) b := iter.New(1, 2, 3) fmt.Println(itertools.Equals(a, b)) }
Output: true
func Filter ¶
Filter returns an iterable that only yields elements from the input iterable for which the given predicate function returns true. The predicate function `fn` is called with each element from the input iterable, and only elements for which the predicate returns true are included in the output iterable.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { result := itertools.Filter(iter.New(1, 2, 3, 4, 5, 6), predicates.Even[int]) itertools.Print(result) }
Output: 2, 4, 6
func Find ¶
Find returns the first element in the iterable that satisfies the given predicate function. If no element satisfies the predicate, it returns the zero value of the element type and false. The iterable must implement the `Next` method to retrieve the next element. The predicate function takes an element of the iterable as input and returns a boolean value indicating whether the element satisfies the condition.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result, _ := itertools.Find(iter.New(1, 2, 3, 4, 5, 6), func(n int) bool { return n > 3 }) fmt.Println(result) }
Output: 4
func Flatten ¶
Flatten returns an iterable that flattens a chain of iterables into a single iterable. It takes a variadic argument `chain` of type `iter.Iterable[iter.Iterable[T]]`, where `T` can be any type. The function returns an iterable of type `iter.Iterable[T]`.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Flatten( iter.New( iter.New(1, 2, 3), iter.New(4, 5, 6), ), iter.New( iter.New(7, 8, 9), ), ) itertools.Print(items) }
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9
func Incrementer ¶
Incrementer returns an iterable that generates a sequence of integers starting from the given start value. Each subsequent value in the sequence is obtained by incrementing the previous value by 1.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Incrementer(0) itertools.Print(itertools.Take(items, 3)) }
Output: 0, 1, 2
func Intersection ¶
func Intersection[K comparable](first, second iter.Iterable[K]) iter.Iterable[K]
Intersection returns an iterable that represents the intersection of two iterables. The returned iterable contains only the elements that are present in both input iterables. The elements are compared using the `==` operator. The input iterables must be of the same type. The order of elements in the returned iterable is not guaranteed.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { first := iter.New(1, 2, 3, 4, 5) second := iter.New(3, 4, 5, 6, 7) items := itertools.Intersection(first, second) itertools.Println(items) }
Output: 3 4 5
func Join ¶
Join concatenates the elements of an iterable into a single iterable, separating each element with a specified separator. The separator can be of any type. The returned iterable will yield the elements of the original iterable, with the separator inserted between each pair of elements.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Join(iter.New(1, 2, 3), 0) itertools.Print(items) }
Output: 1, 0, 2, 0, 3
func Keys ¶
func Keys[K comparable, V any](items map[K]V) iter.Iterable[K]
Keys returns an iterable containing all the keys from the given map. The keys are returned in an arbitrary order. The type parameters K and V specify the key and value types of the map.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { input := map[string]int{ "apple": 5, "banana": 3, "orange": 2, } items := itertools.Keys(input) itertools.Println(items) }
Output: apple banana orange
func Last ¶
Last returns the last element of the given iterable and a boolean value indicating if an element was found. If the iterable is empty, the returned boolean value will be false.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result, _ := itertools.Last(iter.New(1, 2, 3)) fmt.Println(result) }
Output: 3
func LastIf ¶
LastIf returns the last element in the iterable that satisfies the given predicate function. It iterates over the elements of the iterable and returns the last element that satisfies the predicate, along with a boolean value indicating whether such an element was found. If no element satisfies the predicate, the zero value of the element type is returned along with false.
func Lines ¶
Lines returns an iterable that produces each line of text from the given io.Reader. Each line is represented as a string.
Example ¶
package main import ( "strings" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Lines(strings.NewReader("Hello\nWorld\n!")) itertools.Print(items) }
Output: Hello, World, !
func Log ¶
Log is a function that wraps an iterable with logging capabilities. It takes an iterable, a logger, and a format function as input, and returns a new iterable that logs each item before yielding it. The logger is used to log each item, and the format function is used to format the item before logging.
Example ¶
package main import ( "fmt" "log" "os" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { logger := log.New(os.Stdout, "", 0) items := itertools.Log(iter.New(1, 2, 3), logger, func(item int) string { return fmt.Sprintf("Item: %d", item) }) itertools.Drain(items) }
Output: Item: 1 Item: 2 Item: 3
func Map ¶
Map applies a function to each item in the input iterable and returns an iterable of the results. The input iterable can be of any type, and the function can transform each item to a different type. The returned iterable will yield the transformed values.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Map(iter.New(1, 2, 3), func(i int) int { return i * 2 }) itertools.Print(items) }
Output: 2, 4, 6
func Max ¶
func Max[T constraints.Ordered](iter iter.Iterable[T]) T
Max returns the maximum element from the given iterable. The iterable must contain elements of a type that satisfies the Ordered constraint. If the iterable is empty, it returns the zero value of the element type.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Max(iter.New(2, 1, 5, 4, 3)) fmt.Println(result) }
Output: 5
func Min ¶
func Min[T constraints.Ordered](iter iter.Iterable[T]) T
Min returns the minimum element from the given iterable. The iterable must contain elements of a type that satisfies the Ordered constraint. If the iterable is empty, the behavior is undefined.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Min(iter.New(2, 1, 5, 4, 3)) fmt.Println(result) }
Output: 1
func Mix ¶
Mix takes multiple iterables and returns a new iterable that iterates over the elements of each input iterable in a round-robin fashion. The returned iterable will continue until all input iterables are exhausted. The type parameter T represents the type of elements in the input iterables.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Mix( iter.New(1, 2, 3), iter.New(11, 22, 33, 44), iter.New(111, 222), ) itertools.Print(items) }
Output: 1, 11, 111, 2, 22, 222, 3, 33, 44
func Partition ¶
func Partition[T any](input iter.Iterable[T], function predicates.Predicate[T]) (iter.Iterable[T], iter.Iterable[T])
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" "github.com/theMagicalKarp/iter/pkg/predicates" ) func main() { evenItems, oddItems := itertools.Partition(iter.New(1, 2, 3, 4, 5, 6), predicates.Even[int]) itertools.Print(evenItems) itertools.Print(oddItems) }
Output: 2, 4, 6 1, 3, 5
func Print ¶
Print prints the elements of the given iterable to the standard output. It converts each element to a string and joins them with a comma. The elements are printed in the order they are returned by the iterable. After printing all the elements, a newline character is printed.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { itertools.Print(iter.New("a", "b", "c")) }
Output: a, b, c
func Println ¶
Println prints the elements of an iterable to the standard output. It iterates over the elements of the iterable and prints each element on a new line. The type parameter T represents the type of elements in the iterable.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { itertools.Println(iter.New("a", "b", "c")) }
Output: a b c
func Product ¶
Product returns the product of all elements in the given iterable. The iterable must contain elements of a numeric type. If the iterable is empty, the result is 1.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Product(iter.New(1, 2, 3, 4)) fmt.Println(result) }
Output: 24
func Range ¶
Range returns an iterable that generates a sequence of integers from start to stop (exclusive). The start parameter specifies the starting value of the sequence. The stop parameter specifies the ending value of the sequence. The returned iterable can be used in a for loop or with other iterator functions.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Range(0, 5) itertools.Print(items) }
Output: 0, 1, 2, 3, 4
func RangeInc ¶
RangeInc returns an iterable that generates a sequence of integers starting from 'start' up to 'stop' (inclusive), with each integer incremented by 'inc' amount. The 'inc' parameter can be negative to generate a decreasing sequence.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.RangeInc(0, 5, 2) itertools.Print(items) }
Output: 0, 2, 4
Example (Reverse) ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.RangeInc(5, 0, -1) itertools.Print(items) }
Output: 5, 4, 3, 2, 1
func Reader ¶
Reader returns an io.Reader that reads from the given iterable of byte slices. Each call to Read will consume the next byte slice from the iterable.
Example ¶
package main import ( "fmt" "io" "strings" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { iter := iter.New([]byte("Hello"), []byte(" "), []byte("World!")) reader := itertools.Reader(iter) buffer := new(strings.Builder) _, err := io.Copy(buffer, reader) if err != nil { panic(err) } fmt.Println(buffer.String()) }
Output: Hello World!
func Repeat ¶
Repeat returns an iterable that repeatedly yields the specified value. The iterable will continue indefinitely, producing the same value each time.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Repeat("hello world") itertools.Print(itertools.Take(items, 3)) }
Output: hello world, hello world, hello world
func Reverse ¶
Reverse returns an iterable that iterates over the elements of the input iterable in reverse order. The input iterable can be of any type.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Reverse(iter.New(1, 2, 3)) itertools.Print(items) }
Output: 3, 2, 1
func Runes ¶
Runes returns an iterable of runes from the given string value.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Runes("hello world!") itertools.Print(items) }
Output: 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33
func Set ¶
func Set[K comparable](items iter.Iterable[K]) map[K]bool
Set returns a map containing unique elements from the given iterable. The elements in the iterable must be of a comparable type. The returned map contains each unique element as a key, with a value of true.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Set(iter.New(1, 2, 3, 1, 2, 3, 4, 5)) for k := range items { fmt.Println(k) } }
Output: 1 2 3 4 5
func Slice ¶
Slice returns a new slice containing all the elements from the given iterable. The elements are retrieved by calling the Next method on the iterable until it returns false. The type parameter T specifies the type of elements in the iterable.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Slice(iter.New(1, 2, 3, 4, 5)) fmt.Println(items) }
Output: [1 2 3 4 5]
func SliceN ¶
SliceN returns a slice of elements from the given iterable, starting from the specified start index and ending at the specified stop index. The function iterates through the iterable until it reaches the start index, and then appends each element to the returned slice until it reaches the stop index. If the iterable is exhausted before reaching the stop index, the function returns the slice with the available elements. The type parameter T represents the type of elements in the iterable.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.SliceN(iter.New(1, 2, 3, 4, 5), 1, 4) fmt.Println(items) }
Output: [2 3 4]
func Sort ¶
Sort returns an iterable that produces the elements of the input iterable in sorted order. The sorting is performed using the provided comparison function cmp. The input iterable must support iteration and the comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second argument.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Sort(iter.New(2, 1, 5, 4, 3), func(a, b int) int { return a - b }) itertools.Print(items) }
Output: 1, 2, 3, 4, 5
Example (Ascending) ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Sort(iter.New(2, 1, 5, 4, 3), itertools.Asc[int]) itertools.Print(items) }
Output: 1, 2, 3, 4, 5
Example (Descending) ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Sort(iter.New(2, 1, 5, 4, 3), itertools.Desc[int]) itertools.Print(items) }
Output: 5, 4, 3, 2, 1
func StartsWith ¶
func StartsWith[T comparable](iter, prefix iter.Iterable[T]) bool
StartsWith checks if the given iterable starts with the specified prefix. It compares the elements of the iterable and the prefix in order, and returns true if they match. If the prefix is longer than the iterable, it returns false. The elements are compared using the equality operator (==) for the given type T. The iterable and the prefix must both implement the Iterable interface. The type T must satisfy the Ordered constraint, meaning it must be comparable using the comparison operators (<, >, <=, >=).
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := iter.New(1, 2, 3, 4, 5) pre_items := iter.New(1, 2) fmt.Println(itertools.StartsWith(items, pre_items)) }
Output:
func Subtract ¶
func Subtract[K comparable](first, second iter.Iterable[K]) iter.Iterable[K]
Subtract returns an iterable that contains the elements from the first iterable that are not present in the second iterable. The type of elements in the iterables must be comparable.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { first := iter.New(1, 2, 3, 4, 5) second := iter.New(3, 4, 5, 6, 7) items := itertools.Subtract(first, second) itertools.Println(items) }
Output: 1 2
func Sum ¶
func Sum[T constraints.Ordered](iter iter.Iterable[T]) T
Sum returns the sum of all elements in the given iterable. The elements must be of a type that satisfies the Ordered constraint.
Example ¶
package main import ( "fmt" "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { result := itertools.Sum(iter.New(1, 2, 3)) fmt.Println(result) }
Output: 6
func Tail ¶
Tail returns an iterable that yields the last `amount` elements from the given iterable. The `iter` parameter is the iterable to extract elements from. The `amount` parameter specifies the number of elements to extract. The returned iterable will yield the elements in the same order as the original iterable.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Tail(iter.New(1, 2, 3, 4, 5), 2) itertools.Print(items) }
Output: 4, 5
func Take ¶
Take returns a new iterable that yields the first `amount` elements from the given iterable. If the given iterable has fewer than `amount` elements, all elements are returned. The original iterable is not modified.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Take(iter.New(1, 2, 3, 4, 5, 6), 3) itertools.Print(items) }
Output: 1, 2, 3
func Tee ¶
Tee splits an iterable into two separate iterables. Both iterables will produce the same elements as the original iterable. The elements are buffered, so they can be consumed independently. Any changes made to one iterable will not affect the other.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { a, b := itertools.Tee( iter.New(1, 2, 3, 4), ) itertools.Print(a) itertools.Print(b) }
Output: 1, 2, 3, 4 1, 2, 3, 4
func Union ¶
func Union[K comparable](first, second iter.Iterable[K]) iter.Iterable[K]
Union returns an iterable that contains all unique elements from the two input iterables. The elements are returned in the order they are encountered. The input iterables must be of the same type. The type of the elements must be comparable using the == operator.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Union( iter.New(1, 2, 3, 4), iter.New(3, 4, 5, 6), ) itertools.Println(items) }
Output: 1 2 3 4 5 6
func Unique ¶
func Unique[K comparable](input iter.Iterable[K]) iter.Iterable[K]
Unique returns an iterable that contains only the unique elements from the input iterable. The input iterable can contain elements of any type that is comparable. The returned iterable will preserve the order of the unique elements.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Unique(iter.New(1, 2, 2, 3, 3, 3)) itertools.Print(items) }
Output: 1, 2, 3
func Values ¶
func Values[K comparable, V any](items map[K]V) iter.Iterable[V]
Values returns an iterable containing the values of the given map. The keys of the map are ignored. The order of the values in the returned iterable is not guaranteed to be the same as the original map.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { input := map[string]int{ "apple": 5, "banana": 3, "orange": 2, } items := itertools.Values(input) itertools.Println(items) }
Output: 2 3 5
func Zip ¶
func Zip[T any, V any](first iter.Iterable[T], second iter.Iterable[V]) iter.Iterable[tuple.Tuple[T, V]]
Zip returns an iterable that combines elements from two iterables into tuples. The resulting iterable yields tuples where the i-th tuple contains the i-th element from the first iterable and the i-th element from the second iterable. The iterator stops when the shortest input iterable is exhausted.
Example ¶
package main import ( "github.com/theMagicalKarp/iter/pkg/iter" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { items := itertools.Zip( iter.New(1, 2, 3, 4, 5, 6), iter.New("a", "b", "c", "d", "e", "f"), ) itertools.Print(items) }
Output: {1 a}, {2 b}, {3 c}, {4 d}, {5 e}, {6 f}
Types ¶
type AsyncMapFn ¶
AsyncMapFn is a function type that represents an asynchronous mapping operation. It takes a context and an item of type T as input, and returns a value of type V. The context is used for cancellation and deadline propagation across function calls.
type Compare ¶
Compare is a function type that compares two values of type T and returns an integer. It should return a negative value if a < b, 0 if a == b, and a positive value if a > b.
type IterWriteCloser ¶
type IterWriteCloser interface { io.WriteCloser iter.Iterable[[]byte] }
func Writer ¶
func Writer() IterWriteCloser
Writer returns a new instance of io.WriteCloser and iter.Iterable. Data written to this writer is stored to a buffer and then read it back as an iterable of byte slices. The writer can be closed to signal the end of the data, otherwise it will infinity return empty slices. After closing, the writer will continue to return results from the buffer until it is empty, and will not accept any more writes.
Example ¶
package main import ( "fmt" "io" "strings" "github.com/theMagicalKarp/iter/pkg/itertools" ) func main() { writer := itertools.Writer() _, err := io.Copy(writer, strings.NewReader("Hello, World!")) if err != nil { panic(err) } writer.Close() out, _ := writer.Next() fmt.Println(string(out)) }
Output: Hello, World!
type Logger ¶
type Logger interface {
Println(v ...any)
}
Logger is an interface for logging messages.
type MapFn ¶
MapFn is a function type that defines a mapping operation. It takes an input item of type T and returns a transformed value of type V.
type Number ¶
type Number interface { constraints.Integer | constraints.Float }
Number is an interface that represents a numeric value. It can be either an integer or a float.
Source Files ¶
- accumelate.go
- all.go
- any.go
- async.go
- at.go
- batch.go
- chain.go
- count.go
- cycle.go
- drain.go
- drop.go
- each.go
- empty.go
- ends_with.go
- entries.go
- enumerate.go
- equals.go
- filter.go
- find.go
- flatten.go
- incrementer.go
- intersection.go
- join.go
- keys.go
- last.go
- lines.go
- log.go
- map.go
- max.go
- min.go
- mix.go
- pair.go
- partition.go
- print.go
- product.go
- range.go
- reader.go
- repeat.go
- reverse.go
- runes.go
- set.go
- slice.go
- sort.go
- starts_with.go
- subtract.go
- sum.go
- tail.go
- take.go
- tee.go
- union.go
- unique.go
- values.go
- writer.go
- zip.go