-
Notifications
You must be signed in to change notification settings - Fork 10
/
func.go
52 lines (43 loc) · 991 Bytes
/
func.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
package classifier
const defaultBufferSize = 50
// Predicate provides a predicate function
type Predicate func(string) bool
// Mapper provides a map function
type Mapper func(string) string
// Map applies f to each element of the supplied input channel
func Map(vs chan string, f ...Mapper) chan string {
stream := make(chan string, defaultBufferSize)
go func() {
for v := range vs {
for _, fn := range f {
v = fn(v)
}
stream <- v
}
close(stream)
}()
return stream
}
// Filter removes elements from the input channel where the supplied predicate
// is satisfied
// Filter is a Predicate aggregation
func Filter(vs chan string, filters ...Predicate) chan string {
stream := make(chan string, defaultBufferSize)
apply := func(text string) bool {
for _, f := range filters {
if !f(text) {
return false
}
}
return true
}
go func() {
for text := range vs {
if apply(text) {
stream <- text
}
}
close(stream)
}()
return stream
}