lo icon indicating copy to clipboard operation
lo copied to clipboard

Proposal: Add Batch function on slice type, process slice with batch size

Open justfunxin opened this issue 2 years ago • 1 comments

Add Batch function on slice type, process slice with batch size

// Batch process slice in batch
func Batch[T any](s []T, size int, f func([]T, int) error) error {
	total := len(s)
	for i := 0; i < total; i += size {
		upper := i + size
		if upper > total {
			upper = total
		}
		err := f(s[i:upper], i/size)
		if err != nil {
			return err
		}
	}
	return nil
}

Usage:

	var s []string
	for i := 0; i < 10; i++ {
		s = append(s, strconv.Itoa(i))
	}
	err := lo.Batch(s, 3, func(list []string, batch int) error {
		fmt.Printf("process batch:%d, list:%s\n", batch, strings.Join(list, ","))
		return nil
	})

justfunxin avatar May 31 '23 04:05 justfunxin

Use lo.Chunk to implement batch function

func Batch[T any](s []T, size int, f func([]T, int) error) error {
	t := lo.Chunk(s, size)
	for i, v := range t {
		err := f(v, i)
		if err != nil {
			return err
		}
	}
	return nil
}

justfunxin avatar May 31 '23 04:05 justfunxin