Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal : Example of possible implementation of lazy iterators in Go+ (using go_iter) #1400

Open
serge-hulne opened this issue Jul 30, 2023 · 0 comments
Labels

Comments

@serge-hulne
Copy link

Proposal

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.)

@serge-hulne 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants