-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters.go
43 lines (36 loc) · 1.06 KB
/
filters.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
package main
type filtrator func(<-chan Image) <-chan Image
var filters []filtrator
//If filter isn't on, skip. If any of filter parameters is given, filtration is on
func filterInit(opts *FiltOpts, enableLog bool) {
if opts.ScoreF {
filters = append(filters, filterGenerator(func(i Image) bool { return i.Score >= opts.Score }, enableLog))
}
if opts.FavesF {
filters = append(filters, filterGenerator(func(i Image) bool { return i.Faves >= opts.Faves }, enableLog))
}
}
func filterGenerator(filt func(Image) bool, enableLog bool) filtrator {
return func(in <-chan Image) <-chan Image {
out := make(chan Image)
go func() {
for imgdata := range in {
if filt(imgdata) { //Capturing score inside lambda, to prevent passing it around each invocation
out <- imgdata
continue
}
lCondInfo(enableLog, "Filtering ", imgdata.Filename)
}
close(out)
}()
return out
}
}
//FilterChannel cuts off unneeded images
func FilterChannel(in <-chan Image) (out <-chan Image) {
out = in
for _, filter := range filters {
out = filter(out)
}
return
}