go-func/stream/map.go

40 lines
756 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package stream
import "runtime"
//Map 对任意数组操作将A包装成B
func (s *Stream) Map(mappper func(interface{}) interface{}) *Stream {
ret := make([]interface{}, len(s.list))
if s.isParallel {
count := runtime.NumCPU()
ch1 := make(chan interface{}, count)
ch2 := make(chan interface{})
for i := 0; i < count; i++ {
go func() {
for true {
if value, ok := <-ch1; !ok {
break
} else {
ch2 <- mappper(value)
}
}
}()
}
go func() {
for _, v := range s.list {
ch1 <- v
}
close(ch1)
}()
for i := 0; i < len(s.list); i++ {
ret[i] = <-ch2
}
} else {
for i, v := range s.list {
ret[i] = mappper(v)
}
}
s.list = ret
return s
}