You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
647 B
Go
37 lines
647 B
Go
4 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"zero/core/fx"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
result, err := fx.From(func(source chan<- interface{}) {
|
||
|
for i := 0; i < 10; i++ {
|
||
|
source <- i
|
||
|
source <- i
|
||
|
}
|
||
|
}).Map(func(item interface{}) interface{} {
|
||
|
i := item.(int)
|
||
|
return i * i
|
||
|
}).Filter(func(item interface{}) bool {
|
||
|
i := item.(int)
|
||
|
return i%2 == 0
|
||
|
}).Distinct(func(item interface{}) interface{} {
|
||
|
return item
|
||
|
}).Reduce(func(pipe <-chan interface{}) (interface{}, error) {
|
||
|
var result int
|
||
|
for item := range pipe {
|
||
|
i := item.(int)
|
||
|
result += i
|
||
|
}
|
||
|
return result, nil
|
||
|
})
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
} else {
|
||
|
fmt.Println(result)
|
||
|
}
|
||
|
}
|