-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
56 lines (49 loc) · 1.55 KB
/
iterator.go
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
// This file is part of GoIRS.
//
// GoIRS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GoIRS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GoIRS. If not, see <http://www.gnu.org/licenses/>.
package goirs
//StringIterator es un iterador genérico de cadenas
type StringIterator <-chan string
const (
//BUFFERSIZE es el tamaño del buffer de los canales
BUFFERSIZE = 16
)
//Next devuelve el siguiente valor o la cadena vacía en caso de llegar al final
func (i StringIterator) Next() string {
st, ok := <-i
if ok {
return st
}
return ""
}
func (i StringIterator) filter(f func(string) bool, out chan string) {
defer close(out)
for x := range i {
if f(x) {
out <- x
}
}
}
//Filter filtra el contenido del iterador dependiendo de la función f
func (i StringIterator) Filter(f func(string) bool) StringIterator {
k := make(chan string, BUFFERSIZE)
go i.filter(f, k)
return k
}
//Evaluate cierra la cadena y evalua todo lo del iterador
//Básicamente, lo saca todo para forzar las operaciones intermedias
func (i StringIterator) Evaluate() {
for _ = range i {
}
}