Documentation
¶
Overview ¶
Package iter provides functional programming utilities for working with Go 1.23+ iterators. It offers operations for reducing, mapping, concatenating, and transforming iterator sequences in a functional style, compatible with the range-over-func pattern.
Index ¶
- func Append[GA ~func(yield func(A) bool), A any](tail A) func(GA) GA
- func Concat[GT ~func(yield func(T) bool), T any](left, right GT) GT
- func Empty[GA ~func(yield func(A) bool), A any]() GA
- func MapToArray[GA ~func(yield func(A) bool), GB ~[]B, A, B any](f func(A) B) func(GA) GB
- func MapToArrayWithIndex[GA ~func(yield func(A) bool), GB ~[]B, A, B any](f func(int, A) B) func(GA) GB
- func MonadAppend[GA ~func(yield func(A) bool), A any](f GA, tail A) GA
- func MonadMapToArray[GA ~func(yield func(A) bool), GB ~[]B, A, B any](fa GA, f func(A) B) GB
- func MonadMapToArrayWithIndex[GA ~func(yield func(A) bool), GB ~[]B, A, B any](fa GA, f func(int, A) B) GB
- func MonadReduce[GA ~func(yield func(A) bool), A, B any](fa GA, f func(B, A) B, initial B) B
- func MonadReduceWithIndex[GA ~func(yield func(A) bool), A, B any](fa GA, f func(int, B, A) B, initial B) B
- func MonadSequence[GA ~func(yield func(HKTA) bool), HKTA, HKTRA any](fof OfType[HKTA, HKTRA], m M.Monoid[HKTRA], ta GA) HKTRA
- func MonadTraverse[GA ~func(yield func(A) bool), GB ~func(yield func(B) bool), ...](fmap_b func(HKT_B, func(B) GB) HKT_GB, fof_gb OfType[GB, HKT_GB], ...) HKT_GB
- func MonadTraverseReduce[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any](fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, ...) HKTRB
- func MonadTraverseReduceWithIndex[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any](fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, ...) HKTRB
- func MonadTraverseWithIndex[GA ~func(yield func(A) bool), A, HKTB, HKTRB any](fof func(HKTB) HKTRB, m M.Monoid[HKTRB], ta GA, f func(int, A) HKTB) HKTRB
- func Monoid[GA ~func(yield func(A) bool), A any]() M.Monoid[GA]
- func Of[GA ~func(yield func(A) bool), A any](a A) GA
- func Prepend[GA ~func(yield func(A) bool), A any](head A) func(GA) GA
- func Sequence[GA ~func(yield func(HKTA) bool), HKTA, HKTRA any](fof OfType[HKTA, HKTRA], m M.Monoid[HKTRA]) func(GA) HKTRA
- func ToArray[GA ~func(yield func(A) bool), GB ~[]A, A any](fa GA) GB
- func TraverseReduce[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any](fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, ...) func(GA) HKTRB
- func TraverseReduceWithIndex[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any](fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, ...) func(GA) HKTRB
- func TraverseWithIndex[GA ~func(yield func(A) bool), A, HKTB, HKTRB any](fof func(HKTB) HKTRB, m M.Monoid[HKTRB], f func(int, A) HKTB) func(GA) HKTRB
- type ApType
- type Endomorphism
- type Kleisli
- type MapType
- type OfType
- type Seq
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append returns a function that appends a single element to the end of an iterator sequence.
This is the curried version of MonadAppend, useful for partial application and composition.
Parameters:
- tail: The element to append
Returns:
- A function that takes an iterator and returns a new iterator with the tail element appended
Example:
appendThree := Append[func(yield func(int) bool)](3)
iter := func(yield func(int) bool) {
yield(1)
yield(2)
}
result := appendThree(iter) // Yields: 1, 2, 3
func Concat ¶
Concat concatenates two iterator sequences, yielding all elements from left followed by all elements from right.
The resulting iterator will first yield all elements from the left sequence, then all elements from the right sequence. If the consumer stops early (yield returns false), iteration stops immediately without processing remaining elements.
Parameters:
- left: The first iterator sequence
- right: The second iterator sequence
Returns:
- A new iterator that yields elements from both sequences in order
Example:
left := func(yield func(int) bool) {
yield(1)
yield(2)
}
right := func(yield func(int) bool) {
yield(3)
yield(4)
}
combined := Concat(left, right) // Yields: 1, 2, 3, 4
func Empty ¶
Empty creates an empty iterator sequence that yields no elements.
This is the identity element for the Concat operation and represents an empty collection in the iterator context.
Returns:
- An iterator that yields no elements
Example:
iter := Empty[func(yield func(int) bool), int]() // Yields nothing
func MapToArray ¶
MapToArray returns a function that maps each element through a function and collects the results into a slice.
This is the curried version of MonadMapToArray, useful for partial application and composition.
Parameters:
- f: The mapping function to apply to each element
Returns:
- A function that takes an iterator and returns a slice of mapped elements
Example:
double := MapToArray[func(yield func(int) bool), []int](func(x int) int {
return x * 2
})
iter := func(yield func(int) bool) {
yield(1)
yield(2)
}
result := double(iter) // Returns: []int{2, 4}
func MapToArrayWithIndex ¶
func MapToArrayWithIndex[GA ~func(yield func(A) bool), GB ~[]B, A, B any](f func(int, A) B) func(GA) GB
MapToArrayWithIndex returns a function that maps each element through an indexed function and collects the results into a slice.
This is the curried version of MonadMapToArrayWithIndex, useful for partial application and composition.
Parameters:
- f: The mapping function that takes (index, element) and returns the transformed element
Returns:
- A function that takes an iterator and returns a slice of mapped elements
Example:
addIndex := MapToArrayWithIndex[func(yield func(string) bool), []string](
func(i int, s string) string {
return fmt.Sprintf("%d:%s", i, s)
})
iter := func(yield func(string) bool) {
yield("a")
yield("b")
}
result := addIndex(iter) // Returns: []string{"0:a", "1:b"}
func MonadAppend ¶
MonadAppend appends a single element to the end of an iterator sequence.
This creates a new iterator that yields all elements from the original sequence followed by the tail element.
Parameters:
- f: The original iterator sequence
- tail: The element to append
Returns:
- A new iterator with the tail element appended
Example:
iter := func(yield func(int) bool) {
yield(1)
yield(2)
}
result := MonadAppend(iter, 3) // Yields: 1, 2, 3
func MonadMapToArray ¶
MonadMapToArray maps each element of an iterator sequence through a function and collects the results into a slice.
This combines mapping and collection into a single operation, eagerly evaluating the entire iterator sequence and materializing the transformed elements into memory.
Parameters:
- fa: The iterator sequence to map and collect
- f: The mapping function to apply to each element
Returns:
- A slice containing the mapped elements
Example:
iter := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
}
doubled := MonadMapToArray[func(yield func(int) bool), []int](iter, func(x int) int {
return x * 2
}) // Returns: []int{2, 4, 6}
func MonadMapToArrayWithIndex ¶
func MonadMapToArrayWithIndex[GA ~func(yield func(A) bool), GB ~[]B, A, B any](fa GA, f func(int, A) B) GB
MonadMapToArrayWithIndex maps each element of an iterator sequence through a function that receives the element's index, and collects the results into a slice.
This is similar to MonadMapToArray but the mapping function also receives the zero-based index of each element, useful when the position matters in the transformation logic.
Parameters:
- fa: The iterator sequence to map and collect
- f: The mapping function that takes (index, element) and returns the transformed element
Returns:
- A slice containing the mapped elements
Example:
iter := func(yield func(string) bool) {
yield("a")
yield("b")
yield("c")
}
indexed := MonadMapToArrayWithIndex[func(yield func(string) bool), []string](iter,
func(i int, s string) string {
return fmt.Sprintf("%d:%s", i, s)
}) // Returns: []string{"0:a", "1:b", "2:c"}
func MonadReduce ¶
MonadReduce reduces an iterator sequence to a single value using a reducer function.
This is similar to MonadReduceWithIndex but without index tracking, making it more efficient when the position of elements is not needed in the reduction logic.
Parameters:
- fa: The iterator sequence to reduce
- f: The reducer function that takes (accumulator, element) and returns the new accumulator
- initial: The initial value for the accumulator
Returns:
- The final accumulated value after processing all elements
Example:
iter := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
}
sum := MonadReduce(iter, func(acc, val int) int {
return acc + val
}, 0) // Returns: 6
func MonadReduceWithIndex ¶
func MonadReduceWithIndex[GA ~func(yield func(A) bool), A, B any](fa GA, f func(int, B, A) B, initial B) B
MonadReduceWithIndex reduces an iterator sequence to a single value using a reducer function that receives the current index, accumulated value, and current element.
The function iterates through all elements in the sequence, applying the reducer function at each step with the element's index. This is useful when the position of elements matters in the reduction logic.
Parameters:
- fa: The iterator sequence to reduce
- f: The reducer function that takes (index, accumulator, element) and returns the new accumulator
- initial: The initial value for the accumulator
Returns:
- The final accumulated value after processing all elements
Example:
iter := func(yield func(int) bool) {
yield(10)
yield(20)
yield(30)
}
// Sum with index multiplier: 0*10 + 1*20 + 2*30 = 80
result := MonadReduceWithIndex(iter, func(i, acc, val int) int {
return acc + i*val
}, 0)
func MonadSequence ¶
func MonadSequence[GA ~func(yield func(HKTA) bool), HKTA, HKTRA any]( fof OfType[HKTA, HKTRA], m M.Monoid[HKTRA], ta GA) HKTRA
MonadSequence sequences an iterator of effects into an effect containing an iterator.
This is a special case of traverse where the transformation function is the identity. It "flips" the nesting of the iterator and effect types, collecting all effects into a single effect containing an iterator of values.
Type Parameters:
- GA: The input iterator type ~func(yield func(HKTA) bool)
- HKTA: The higher-kinded type representing an effect containing A
- HKTRA: The higher-kinded type representing an effect containing an iterator of A
Parameters:
- fof: Lifts an HKTA value into the HKTRA context
- m: A monoid for combining HKTRA values
- ta: The input iterator of effects to sequence
Returns:
- An effect containing an iterator of values
Example (conceptual with Option):
iter := func(yield func(Option[int]) bool) {
yield(Some(1))
yield(Some(2))
yield(Some(3))
}
result := MonadSequence(..., iter) // Some(iterator of [1,2,3])
iter2 := func(yield func(Option[int]) bool) {
yield(Some(1))
yield(None)
}
result2 := MonadSequence(..., iter2) // None
func MonadTraverse ¶
func MonadTraverse[GA ~func(yield func(A) bool), GB ~func(yield func(B) bool), A, B, HKT_B, HKT_GB_GB, HKT_GB any]( fmap_b func(HKT_B, func(B) GB) HKT_GB, fof_gb OfType[GB, HKT_GB], fmap_gb func(HKT_GB, func(GB) func(GB) GB) HKT_GB_GB, fap_gb func(HKT_GB_GB, HKT_GB) HKT_GB, ta GA, f func(A) HKT_B) HKT_GB
MonadTraverse traverses an iterator sequence, applying an effectful function to each element and collecting the results in an applicative context.
This is a fundamental operation in functional programming that allows you to "turn inside out" a structure containing effects. It maps each element through a function that produces an effect, then sequences all those effects together while preserving the iterator structure.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- GB: The output iterator type ~func(yield func(B) bool)
- A: The input element type
- B: The output element type
- HKT_B: The higher-kinded type representing an effect containing B
- HKT_GB_GB: The higher-kinded type for a function from GB to GB in the effect context
- HKT_GB: The higher-kinded type representing an effect containing GB (the result iterator)
Parameters:
- fmap_b: Maps a function over HKT_B to produce HKT_GB
- fof_gb: Lifts a GB value into the effect context (pure/of operation)
- fmap_gb: Maps a function over HKT_GB to produce HKT_GB_GB
- fap_gb: Applies an effectful function to an effectful value (ap operation)
- ta: The input iterator sequence to traverse
- f: The effectful function to apply to each element
Returns:
- An effect containing an iterator of transformed elements
Note: We need to pass the applicative operations explicitly because Go doesn't support higher-kinded types or template methods on structs/interfaces.
Example (conceptual with Option):
// Traverse an iterator of strings, parsing each as an integer
// If any parse fails, the whole result is None
iter := func(yield func(string) bool) {
yield("1")
yield("2")
yield("3")
}
result := MonadTraverse(..., iter, parseInt) // Some(iterator of [1,2,3]) or None
func MonadTraverseReduce ¶
func MonadTraverseReduce[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any]( fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, ta GA, transform func(A) HKTB, reduce func(GB, B) GB, initial GB, ) HKTRB
MonadTraverseReduce combines traversal with reduction, applying an effectful transformation and accumulating results using a reducer function.
This is a more efficient operation when you want to both transform elements through effects and reduce them to a single accumulated value, avoiding intermediate collections.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- GB: The accumulator type
- A: The input element type
- B: The transformed element type
- HKTB: The higher-kinded type representing an effect containing B
- HKTAB: The higher-kinded type for a function from B to GB in the effect context
- HKTRB: The higher-kinded type representing an effect containing GB
Parameters:
- fof: Lifts a GB value into the effect context
- fmap: Maps a function over the effect to produce an effectful function
- fap: Applies an effectful function to an effectful value
- ta: The input iterator sequence to traverse and reduce
- transform: The effectful function to apply to each element
- reduce: The reducer function that combines the accumulator with a transformed element
- initial: The initial accumulator value
Returns:
- An effect containing the final accumulated value
Example (conceptual):
iter := func(yield func(string) bool) { yield("1"); yield("2"); yield("3") }
// Parse strings to ints and sum them
result := MonadTraverseReduce(..., iter, parseInt, add, 0)
// Returns: Some(6) or None if any parse fails
func MonadTraverseReduceWithIndex ¶
func MonadTraverseReduceWithIndex[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any]( fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, ta GA, transform func(int, A) HKTB, reduce func(GB, B) GB, initial GB, ) HKTRB
MonadTraverseReduceWithIndex combines indexed traversal with reduction, applying an effectful transformation that receives element indices and accumulating results using a reducer function.
This is similar to MonadTraverseReduce but the transformation function also receives the zero-based index of each element, useful when position matters in the transformation logic.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- GB: The accumulator type
- A: The input element type
- B: The transformed element type
- HKTB: The higher-kinded type representing an effect containing B
- HKTAB: The higher-kinded type for a function from B to GB in the effect context
- HKTRB: The higher-kinded type representing an effect containing GB
Parameters:
- fof: Lifts a GB value into the effect context
- fmap: Maps a function over the effect to produce an effectful function
- fap: Applies an effectful function to an effectful value
- ta: The input iterator sequence to traverse and reduce
- transform: The effectful function that takes (index, element) and returns an effect
- reduce: The reducer function that combines the accumulator with a transformed element
- initial: The initial accumulator value
Returns:
- An effect containing the final accumulated value
Example (conceptual):
iter := func(yield func(string) bool) { yield("a"); yield("b"); yield("c") }
// Create indexed strings and concatenate
result := MonadTraverseReduceWithIndex(..., iter,
func(i int, s string) Effect[string] {
return Pure(fmt.Sprintf("%d:%s", i, s))
},
func(acc, s string) string { return acc + "," + s },
"")
// Returns: Effect containing "0:a,1:b,2:c"
func MonadTraverseWithIndex ¶
func MonadTraverseWithIndex[GA ~func(yield func(A) bool), A, HKTB, HKTRB any]( fof func(HKTB) HKTRB, m M.Monoid[HKTRB], ta GA, f func(int, A) HKTB) HKTRB
MonadTraverseWithIndex traverses an iterator sequence with index tracking, applying an effectful function to each element along with its index.
This is similar to MonadTraverse but the transformation function receives both the element's zero-based index and the element itself, useful when the position matters in the transformation.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- A: The input element type
- HKTB: The higher-kinded type representing an effect containing B
- HKTRB: The higher-kinded type representing an effect containing an iterator of B
Parameters:
- fof: Lifts an HKTB value into the HKTRB context
- m: A monoid for combining HKTRB values
- ta: The input iterator sequence to traverse
- f: The effectful function that takes (index, element) and returns an effect
Returns:
- An effect containing an iterator of transformed elements
Example (conceptual):
iter := func(yield func(string) bool) {
yield("a")
yield("b")
}
// Add index prefix to each element
result := MonadTraverseWithIndex(..., iter, func(i int, s string) Effect[string] {
return Pure(fmt.Sprintf("%d:%s", i, s))
}) // Effect containing iterator of ["0:a", "1:b"]
func Monoid ¶
Monoid returns a Monoid instance for iterator sequences.
The monoid uses Concat as the binary operation and Empty as the identity element, allowing iterator sequences to be combined in an associative way with a neutral element. This enables generic operations that work with any monoid, such as folding a collection of iterators into a single iterator.
Returns:
- A Monoid instance with Concat and Empty operations
Example:
m := Monoid[func(yield func(int) bool), int]()
iter1 := func(yield func(int) bool) { yield(1); yield(2) }
iter2 := func(yield func(int) bool) { yield(3); yield(4) }
combined := m.Concat(iter1, iter2) // Yields: 1, 2, 3, 4
empty := m.Empty() // Yields nothing
func Of ¶
Of creates an iterator sequence containing a single element.
This is the unit/return operation for the iterator monad, lifting a single value into the iterator context.
Parameters:
- a: The element to wrap in an iterator
Returns:
- An iterator that yields exactly one element
Example:
iter := Of[func(yield func(int) bool)](42) // Yields: 42
func Prepend ¶
Prepend returns a function that prepends a single element to the beginning of an iterator sequence.
This is the curried version for prepending, useful for partial application and composition.
Parameters:
- head: The element to prepend
Returns:
- A function that takes an iterator and returns a new iterator with the head element prepended
Example:
prependZero := Prepend[func(yield func(int) bool)](0)
iter := func(yield func(int) bool) {
yield(1)
yield(2)
}
result := prependZero(iter) // Yields: 0, 1, 2
func Sequence ¶
func Sequence[GA ~func(yield func(HKTA) bool), HKTA, HKTRA any]( fof OfType[HKTA, HKTRA], m M.Monoid[HKTRA]) func(GA) HKTRA
Sequence is the curried version of MonadSequence, returning a function that sequences an iterator of effects.
This version is more suitable for partial application and function composition.
Type Parameters:
- GA: The input iterator type ~func(yield func(HKTA) bool)
- HKTA: The higher-kinded type representing an effect containing A
- HKTRA: The higher-kinded type representing an effect containing an iterator of A
Parameters:
- fof: Lifts an HKTA value into the HKTRA context
- m: A monoid for combining HKTRA values
Returns:
- A function that takes an iterator of effects and returns an effect containing an iterator
Example (conceptual):
sequenceOptions := Sequence[...](fof, monoid)
iter := func(yield func(Option[int]) bool) { yield(Some(1)); yield(Some(2)) }
result := sequenceOptions(iter) // Some(iterator of [1,2])
func ToArray ¶
ToArray collects all elements from an iterator sequence into a slice.
This eagerly evaluates the entire iterator sequence and materializes all elements into memory as a slice.
Parameters:
- fa: The iterator sequence to collect
Returns:
- A slice containing all elements from the iterator
Example:
iter := func(yield func(int) bool) {
yield(1)
yield(2)
yield(3)
}
arr := ToArray[func(yield func(int) bool), []int](iter) // Returns: []int{1, 2, 3}
func TraverseReduce ¶
func TraverseReduce[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any]( fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, transform func(A) HKTB, reduce func(GB, B) GB, initial GB, ) func(GA) HKTRB
TraverseReduce is the curried version of MonadTraverseReduce, returning a function that traverses and reduces an iterator.
This version is more suitable for partial application and function composition.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- GB: The accumulator type
- A: The input element type
- B: The transformed element type
- HKTB: The higher-kinded type representing an effect containing B
- HKTAB: The higher-kinded type for a function from B to GB in the effect context
- HKTRB: The higher-kinded type representing an effect containing GB
Parameters:
- fof: Lifts a GB value into the effect context
- fmap: Maps a function over the effect to produce an effectful function
- fap: Applies an effectful function to an effectful value
- transform: The effectful function to apply to each element
- reduce: The reducer function that combines the accumulator with a transformed element
- initial: The initial accumulator value
Returns:
- A function that takes an iterator and returns an effect containing the accumulated value
Example (conceptual):
sumParsedInts := TraverseReduce[...](fof, fmap, fap, parseInt, add, 0)
iter := func(yield func(string) bool) { yield("1"); yield("2"); yield("3") }
result := sumParsedInts(iter) // Some(6) or None if any parse fails
func TraverseReduceWithIndex ¶
func TraverseReduceWithIndex[GA ~func(yield func(A) bool), GB, A, B, HKTB, HKTAB, HKTRB any]( fof func(GB) HKTRB, fmap func(func(GB) func(B) GB) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, transform func(int, A) HKTB, reduce func(GB, B) GB, initial GB, ) func(GA) HKTRB
TraverseReduceWithIndex is the curried version of MonadTraverseReduceWithIndex, returning a function that traverses and reduces an iterator with index tracking.
This version is more suitable for partial application and function composition.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- GB: The accumulator type
- A: The input element type
- B: The transformed element type
- HKTB: The higher-kinded type representing an effect containing B
- HKTAB: The higher-kinded type for a function from B to GB in the effect context
- HKTRB: The higher-kinded type representing an effect containing GB
Parameters:
- fof: Lifts a GB value into the effect context
- fmap: Maps a function over the effect to produce an effectful function
- fap: Applies an effectful function to an effectful value
- transform: The effectful function that takes (index, element) and returns an effect
- reduce: The reducer function that combines the accumulator with a transformed element
- initial: The initial accumulator value
Returns:
- A function that takes an iterator and returns an effect containing the accumulated value
Example (conceptual):
concatIndexed := TraverseReduceWithIndex[...](fof, fmap, fap,
func(i int, s string) Effect[string] {
return Pure(fmt.Sprintf("%d:%s", i, s))
},
func(acc, s string) string { return acc + "," + s },
"")
iter := func(yield func(string) bool) { yield("a"); yield("b") }
result := concatIndexed(iter) // Effect containing "0:a,1:b"
func TraverseWithIndex ¶
func TraverseWithIndex[GA ~func(yield func(A) bool), A, HKTB, HKTRB any]( fof func(HKTB) HKTRB, m M.Monoid[HKTRB], f func(int, A) HKTB) func(GA) HKTRB
TraverseWithIndex is the curried version of MonadTraverseWithIndex, returning a function that traverses an iterator with index tracking.
This version is more suitable for partial application and function composition.
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- A: The input element type
- HKTB: The higher-kinded type representing an effect containing B
- HKTRB: The higher-kinded type representing an effect containing an iterator of B
Parameters:
- fof: Lifts an HKTB value into the HKTRB context
- m: A monoid for combining HKTRB values
- f: The effectful function that takes (index, element) and returns an effect
Returns:
- A function that takes an iterator and returns an effect containing an iterator of transformed elements
Example (conceptual):
addIndexPrefix := TraverseWithIndex[...](fof, monoid, func(i int, s string) Effect[string] {
return Pure(fmt.Sprintf("%d:%s", i, s))
})
iter := func(yield func(string) bool) { yield("a"); yield("b") }
result := addIndexPrefix(iter) // Effect containing iterator of ["0:a", "1:b"]
Types ¶
type Endomorphism ¶ added in v2.2.1
type Endomorphism[A any] = endomorphism.Endomorphism[A]
type Kleisli ¶ added in v2.2.1
type Kleisli[A, HKT_B any] = func(A) HKT_B
func Traverse ¶
func Traverse[GA ~func(yield func(A) bool), GB ~func(yield func(B) bool), A, B, HKT_B, HKT_GB_GB, HKT_GB any]( fmap_b MapType[B, GB, HKT_B, HKT_GB], fof_gb OfType[GB, HKT_GB], fmap_gb MapType[GB, Endomorphism[GB], HKT_GB, HKT_GB_GB], fap_gb ApType[HKT_GB, HKT_GB, HKT_GB_GB], f Kleisli[A, HKT_B]) Kleisli[GA, HKT_GB]
Traverse is the curried version of MonadTraverse, returning a function that traverses an iterator.
This version uses type aliases for better readability and is more suitable for partial application and function composition. It returns a Kleisli arrow (a function from GA to HKT_GB).
Type Parameters:
- GA: The input iterator type ~func(yield func(A) bool)
- GB: The output iterator type ~func(yield func(B) bool)
- A: The input element type
- B: The output element type
- HKT_B: The higher-kinded type representing an effect containing B
- HKT_GB_GB: The higher-kinded type for a function from GB to GB in the effect context
- HKT_GB: The higher-kinded type representing an effect containing GB
Parameters:
- fmap_b: Maps a function over HKT_B to produce HKT_GB
- fof_gb: Lifts a GB value into the effect context
- fmap_gb: Maps a function over HKT_GB to produce HKT_GB_GB
- fap_gb: Applies an effectful function to an effectful value
- f: The effectful function to apply to each element (Kleisli arrow)
Returns:
- A function that takes an iterator and returns an effect containing an iterator of transformed elements
Example (conceptual):
parseInts := Traverse[...](fmap, fof, fmap_gb, fap, parseInt)
iter := func(yield func(string) bool) { yield("1"); yield("2") }
result := parseInts(iter) // Effect containing iterator of integers