MCPcopy Create free account
hub / github.com/astercloud/aster / TransformSimple

Function TransformSimple

pkg/stream/stream.go:572–603  ·  view source on GitHub ↗

TransformSimple 创建新的 goroutine 进行转换

(r *Reader[T], fn func(T) (U, error))

Source from the content-addressed store, hash-verified

570
571// TransformSimple 创建新的 goroutine 进行转换
572func TransformSimple[T, U any](r *Reader[T], fn func(T) (U, error)) *Reader[U] {
573 out, writer := Pipe[U](5)
574
575 go func() {
576 defer writer.Close()
577 for {
578 v, err := r.Recv()
579 if errors.Is(err, io.EOF) {
580 return
581 }
582 if err != nil {
583 writer.Send(*new(U), err)
584 return
585 }
586
587 result, err := fn(v)
588 if errors.Is(err, ErrSkip) {
589 continue
590 }
591 if err != nil {
592 writer.Send(*new(U), err)
593 return
594 }
595
596 if writer.Send(result, nil) {
597 return
598 }
599 }
600 }()
601
602 return out
603}
604
605// Filter 创建只包含匹配谓词的值的新读取器
606func Filter[T any](r *Reader[T], predicate func(T) bool) *Reader[T] {

Callers 3

TransformFunction · 0.85
FilterFunction · 0.85
MapFunction · 0.85

Calls 3

RecvMethod · 0.80
CloseMethod · 0.65
SendMethod · 0.45

Tested by

no test coverage detected