Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Enumerator could be more convenient to use #26

Open
rogpeppe opened this issue Jul 18, 2015 · 2 comments
Open

Enumerator could be more convenient to use #26

rogpeppe opened this issue Jul 18, 2015 · 2 comments

Comments

@rogpeppe
Copy link

A standard idiom for enumerators seems to have emerged [1],[2],
which makes for quite nice looking code. Perhaps consider
using that here.

Something like this perhaps?

type Enumerator struct {...}

// Next moves to the next element in the enumeration.
// It returns false when there are no more entries
// or if there's an error.
func (*Enumerator) Next() bool

// Prev moves to the previous element in the enumeration.
// It returns false when there are no more entries
// or if there's an error.
func (*Enumerator) Prev() bool

// Key returns the key at the current enumerator position.
func (*Enumerator) Key() []byte

// Value returns the value at the current enumerator position.
func (*Enumerator) Value() []byte

// Err returns any error encountered during the enumeration.
func (*Enumerator) Err() error

Example usage:

enum := db.SeekFirst()
for enum.Next() {
    fmt.Printf("%s=%s\n", enum.Key(), enum.Value())
}
if err := enum.Err(); err != nil {
    log.Fatal(err)
}

[1] http://golang.org/pkg/bufio/#Scanner
[2] http://godoc.org/gopkg.in/mgo.v2#Iter

@cznic
Copy link
Owner

cznic commented Jul 19, 2015

@rogpeppe Thanks for the suggestions. Could you please checkout branch issue25 (should have been named issue26, but I errored) and test and review the changes? I would like to know your opinions before publishing it on master. Thanks in advance.

@rogpeppe
Copy link
Author

I've commented directly on cf4d15f
(BTW, why not just make a PR?)

Seems reasonable to me but I'm not keen on the API pollution.

Thanks for considering the issue.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants