1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-11-23 22:14:53 +02:00
Files
fp-go/v2/iterator/stateless/seq.go
Dr. Carsten Leue 2c1d8196b4 fix: support go iterators and cleanup types
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2025-11-14 12:56:12 +01:00

30 lines
717 B
Go

package stateless
import (
O "github.com/IBM/fp-go/v2/option"
P "github.com/IBM/fp-go/v2/pair"
)
// ToSeq converts the stateless [Iterator] to an idiomatic go iterator
func ToSeq[T any](it Iterator[T]) Seq[T] {
current := Current[T]
return func(yield Predicate[T]) {
next, ok := O.Unwrap(it())
for ok && yield(current(next)) {
next, ok = O.Unwrap(Next(next)())
}
}
}
// ToSeq2 converts the stateless [Iterator] to an idiomatic go iterator
func ToSeq2[K, V any](it Iterator[Pair[K, V]]) Seq2[K, V] {
current := Current[Pair[K, V]]
return func(yield func(K, V) bool) {
yp := P.Paired(yield)
next, ok := O.Unwrap(it())
for ok && yp(current(next)) {
next, ok = O.Unwrap(Next(next)())
}
}
}