Skip to content
Prev Previous commit
Next Next commit
added comments
  • Loading branch information
Ron Koehler committed May 15, 2023
commit 11cbc39ab9c46ae486cb8e4393e5abaab56cd5ef
22 changes: 17 additions & 5 deletions iter/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,32 @@ func (iter Iterator[T]) ForEachIdx(input []T, f func(int, *T)) {
})
}

func ForEachIdxCtx[T any](octx context.Context, input []T, f func(context.Context, int, *T) error) error {
return Iterator[T]{}.ForEachIdxCtx(octx, input, f)
}

// ForEachCtx is the same as ForEach except it also accepts a context
// that it uses to manages the execution of tasks.
// The context is cancelled on task failure and the first error is returned
func ForEachCtx[T any](octx context.Context, input []T, f func(context.Context, *T) error) error {
return Iterator[T]{}.ForEachCtx(octx, input, f)
}

// ForEachCtx is the same as ForEach except it also accepts a context
// that it uses to manages the execution of tasks.
// The context is cancelled on task failure and the first error is returned
func (iter Iterator[T]) ForEachCtx(octx context.Context, input []T, f func(context.Context, *T) error) error {
return iter.ForEachIdxCtx(octx, input, func(ctx context.Context, _ int, input *T) error {
return f(ctx, input)
})
}

// ForEachIdxCtx is the same as ForEachIdx except it also accepts a context
// that it uses to manages the execution of tasks.
// The context is cancelled on task failure and the first error is returned
func ForEachIdxCtx[T any](octx context.Context, input []T, f func(context.Context, int, *T) error) error {
return Iterator[T]{}.ForEachIdxCtx(octx, input, f)
}

// ForEachIdxCtx is the same as ForEachIdx except it also accepts a context
// that it uses to manages the execution of tasks.
// The context is cancelled on task failure and the first error is returned
func (iter Iterator[T]) ForEachIdxCtx(octx context.Context, input []T, f func(context.Context, int, *T) error) error {
if iter.MaxGoroutines == 0 {
// iter is a value receiver and is hence safe to mutate
Expand All @@ -99,7 +111,7 @@ func (iter Iterator[T]) ForEachIdxCtx(octx context.Context, input []T, f func(co
return err
}
}
return nil
return ctx.Err() // nil if the context was never cancelled
}

runner := pool.New().
Expand Down
6 changes: 6 additions & 0 deletions iter/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ func (m Mapper[T, R]) MapErr(input []T, f func(*T) (R, error)) ([]R, error) {
return res, errs
}

// MapErrCtx is the same as MapErr except it also accepts a context
// that it uses to manages the execution of tasks.
// The context is cancelled on task failure and the first error is returned
func MapErrCtx[T, R any](octx context.Context, input []T, f func(context.Context, *T) (R, error)) ([]R, error) {
Comment thread
rkoehl05 marked this conversation as resolved.
Outdated
return Mapper[T, R]{}.MapErrCtx(octx, input, f)
}

// MapErrCtx is the same as MapErr except it also accepts a context
// that it uses to manages the execution of tasks.
// The context is cancelled on task failure and the first error is returned
func (m Mapper[T, R]) MapErrCtx(octx context.Context, input []T, f func(context.Context, *T) (R, error)) ([]R, error) {
var (
res = make([]R, len(input))
Expand Down