-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Tap* function signature and always forward the T type
- Loading branch information
Showing
3 changed files
with
46 additions
and
12 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,47 @@ | ||
package pipes | ||
|
||
func Tap[T any](size int, tap func(T) T, in <-chan T) ChanPull[T] { | ||
return Map(size, tap, in) | ||
func Tap[T any](size int, tap func(T), in <-chan T) ChanPull[T] { | ||
out := make(chan T, size) | ||
go tapWorker(tap, in, out) | ||
return out | ||
} | ||
|
||
func TapWithError[T any](size int, tap func(T) (T, error), in <-chan T) (ChanPull[T], ChanPull[error]) { | ||
return MapWithError(size, tap, in) | ||
func tapWorker[T any](tap func(T), in <-chan T, out chan<- T) { | ||
defer close(out) | ||
for t := range in { | ||
tap(t) | ||
out <- t | ||
} | ||
} | ||
|
||
func TapWithErrorSink[T any](size int, tap func(T) (T, error), sink func(error), in <-chan T) ChanPull[T] { | ||
return MapWithErrorSink(size, tap, sink, in) | ||
func TapWithError[T any](size int, tap func(T) error, in <-chan T) (ChanPull[T], ChanPull[error]) { | ||
out, err := make(chan T, size), make(chan error, size) | ||
go tapWithErrorWorker(tap, in, out, err) | ||
return out, err | ||
} | ||
|
||
func tapWithErrorWorker[T any](tap func(T) error, in <-chan T, out chan<- T, err chan<- error) { | ||
defer func() { close(out); close(err) }() | ||
for t := range in { | ||
if er := tap(t); er != nil { | ||
err <- er | ||
} | ||
out <- t | ||
} | ||
} | ||
|
||
func TapWithErrorSink[T any](size int, tap func(T) error, sink func(error), in <-chan T) ChanPull[T] { | ||
out := make(chan T, size) | ||
go tapWithErrorSinkWorker(tap, sink, in, out) | ||
return out | ||
} | ||
|
||
func tapWithErrorSinkWorker[T any](mp func(T) error, sink func(error), in <-chan T, out chan<- T) { | ||
defer close(out) | ||
for t := range in { | ||
if er := mp(t); er != nil { | ||
sink(er) | ||
} | ||
out <- t | ||
} | ||
} |