You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You could integrate go_iter in Go+, so as to handle streams of arbitrary large length:
package main
import (
. "github.com/serge-hulne/go_iter"
)
// Defining a generator interface (and its three mehods Next(), HasNext() and Value()) :
type Counter struct {
value int
}
func (c *Counter) Next() {
if c.HasNext() {
c.value += 1
}
}
func (c *Counter) HasNext() bool {
if c.Value() < 10 {
return true
} else {
return false
}
}
func (c *Counter) Value() int {
return c.value
}
// Demonstrating Map, Filter, etc
func main() {
// Map
c := Counter{0}
println "Map:"
it := Generator_to_Iterator[int](&c)
it1 := Map[int](it, func(x int) int {
return 2 * x
})
for i <- it1 {
println i
}
println "- - - "
// Filter:
c = Counter{0}
println "Filter:"
it = Generator_to_Iterator[int](&c)
it1 = Filter(it, func(x int) bool {
return x%2 == 0
})
for i <- it1 {
println i
}
}
Background
As far as I know, so far, Go+ only offers list comprehensions for arrays and slices, not channels of data of arbitrary, possibly infinite length.
Workarounds
By integrating go_iter or a variation thereof in Go+, the users's code would not have to load a very large array at once in memory, it would be handle one chunk at a time (for instance, if the array is read from a file, line by line, etc.)
The text was updated successfully, but these errors were encountered:
serge-hulne
changed the title
Example of possible implementation of lazy iterators in Go+ (using go_iter)
Proposal : Example of possible implementation of lazy iterators in Go+ (using go_iter)
Jul 30, 2023
Proposal
You could integrate go_iter in Go+, so as to handle streams of arbitrary large length:
Background
As far as I know, so far, Go+ only offers list comprehensions for arrays and slices, not channels of data of arbitrary, possibly infinite length.
Workarounds
By integrating go_iter or a variation thereof in Go+, the users's code would not have to load a very large array at once in memory, it would be handle one chunk at a time (for instance, if the array is read from a file, line by line, etc.)
The text was updated successfully, but these errors were encountered: