-
-
Save mytrile/5452789 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "unicode/utf8" | |
| ) | |
| type Item string | |
| type Stream chan Item | |
| type Acc string | |
| func NewWordStream(words []Item) Stream { | |
| stream := make(Stream) | |
| go func() { | |
| for _, w := range words { | |
| stream <- w | |
| } | |
| close(stream) | |
| }() | |
| return stream | |
| } | |
| func (in Stream) Map(fn func(Item) Item) Stream { | |
| out := make(Stream) | |
| go func() { | |
| for x := range in { | |
| out <- fn(x) | |
| } | |
| close(out) | |
| }() | |
| return out | |
| } | |
| func (in Stream) Filter(fn func(Item) bool) Stream { | |
| out := make(Stream) | |
| go func() { | |
| for x := range in { | |
| if fn(x) { | |
| out <- x | |
| } | |
| } | |
| close(out) | |
| }() | |
| return out | |
| } | |
| func (in Stream) Reduce(fn func(Acc, Item) (Acc, Acc)) chan Acc { | |
| out := make(chan Acc) | |
| go func() { | |
| var val, acc Acc | |
| for x := range in { | |
| acc, val = fn(acc, x) | |
| if val != "" { | |
| out <- val | |
| } | |
| } | |
| close(out) | |
| }() | |
| return out | |
| } | |
| func (in Stream) Sniff(fn func(Item)) Stream { | |
| out := make(Stream) | |
| go func() { | |
| for x := range in { | |
| fn(x) | |
| out <- x | |
| } | |
| close(out) | |
| }() | |
| return out | |
| } | |
| func (in Stream) Split() (Stream, Stream) { | |
| out1, out2 := make(Stream), make(Stream) | |
| go func() { | |
| for x := range in { | |
| out1 <- x | |
| out2 <- x | |
| } | |
| close(out1) | |
| close(out2) | |
| }() | |
| return out1, out2 | |
| } | |
| func (in Stream) ReduceAll(fn func(Acc, Item) Acc) Acc { | |
| var acc Acc | |
| for x := range in { | |
| acc = fn(acc, x) | |
| } | |
| return acc | |
| } | |
| func (in Stream) Collect() []Item { | |
| a := make([]Item, 0, 5) | |
| for x := range in { | |
| a = append(a, x) | |
| } | |
| return a | |
| } | |
| func (in Stream) Do(fn func(Item)) { | |
| for x := range in { | |
| fn(x) | |
| } | |
| } | |
| func main() { | |
| reverse := func(word Item) Item { | |
| s := string(word) | |
| o := make([]rune, utf8.RuneCountInString(s)) | |
| i := len(o) | |
| for _, c := range s { | |
| i-- | |
| o[i] = c | |
| } | |
| return Item(o) | |
| } | |
| longerThan := func(n int) func(Item) bool { | |
| return func(w Item) bool { | |
| return len(w) > n | |
| } | |
| } | |
| not := func(s string) func(Item) bool { | |
| item := Item(s) | |
| return func(w Item) bool { | |
| return item != w | |
| } | |
| } | |
| words := []Item{"this", "is", "pretty", "cool", "1", "2", "3"} | |
| NewWordStream(words).Map(reverse).Filter(longerThan(2)).Map(reverse).Filter(not("this")).Do(func(w Item) { | |
| println(w) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment