-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking changes: improved customization support
- Loading branch information
1 parent
1cd7f48
commit df74ede
Showing
5 changed files
with
102 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.3.1 | ||
version=0.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,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 slice | ||
func Map(vs chan string, f Mapper) chan string { | ||
outstream := make(chan 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 { | ||
outstream <- f(v) | ||
for _, fn := range f { | ||
v = fn(v) | ||
} | ||
stream <- v | ||
} | ||
close(outstream) | ||
close(stream) | ||
}() | ||
return outstream | ||
|
||
return stream | ||
} | ||
|
||
// Filter removes elements from the input slice where the supplied predicate | ||
// Filter removes elements from the input channel where the supplied predicate | ||
// is satisfied | ||
func Filter(vs chan string, f Predicate) chan string { | ||
outstream := make(chan string) | ||
// 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 v := range vs { | ||
if f(v) { | ||
outstream <- v | ||
for text := range vs { | ||
if apply(text) { | ||
stream <- text | ||
} | ||
} | ||
close(outstream) | ||
close(stream) | ||
}() | ||
return outstream | ||
} | ||
|
||
return stream | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters