aboutsummaryrefslogtreecommitdiffstats
path: root/internal/tag/tag.go
blob: b4b627df08ca8dd07d6874d50b6018346a5d8137 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package tag

import "context"

type Tagged[V, T any] struct {
	Val V
	Tag T
}

func Tag[V, T any](ctx context.Context, out chan<- Tagged[V, T], in <-chan V, f func(V) T) {
	for {
		select {
		case val, ok := <-in:
			if !ok {
				return
			}
			out <- Tagged[V, T]{val, f(val)}
		case <-ctx.Done():
			return
		}
	}
}