-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerated_string_iterator.go
More file actions
58 lines (46 loc) · 1.42 KB
/
Copy pathgenerated_string_iterator.go
File metadata and controls
58 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Code generated by go-codegen(https://github.com/nchern/go-codegen).
// You COULD edit this code it you really need it and know what are you doing
// Package iterator provides a built-in implementation of an ideomatic generic iterator
package main
// T0 is a generic type variable placeholder of an iterator element. It will not appear in the generated code
// StringGeneratorFunc is a function that should generate elements and send them to a given channel
type StringGeneratorFunc func(generator chan<- string) error
// StringSliceGenerator generates elements from a given slice
func StringSliceGenerator(src []string) StringGeneratorFunc {
return func(iter chan<- string) error {
for _, v := range src {
iter <- v
}
return nil
}
}
// StringIter implements iterator over String type elements
type StringIter interface {
// Err returns error if it happened during generation
Err() error
// Next returns next element from iterator
Next() <-chan string
}
type iter struct {
err error
iter chan string
}
// Err returns error if it happened during generation
func (i *iter) Err() error {
return i.err
}
// Next returns next element from iterator
func (i *iter) Next() <-chan string {
return i.iter
}
// Generate creates Iterator from generator func
func Generate(f StringGeneratorFunc) StringIter {
gen := &iter{
iter: make(chan string),
}
go func() {
gen.err = f(gen.iter)
close(gen.iter)
}()
return gen
}