Skip to content

[BUG] Does not return the last item in the batch list#2

Open
candidosales wants to merge 2 commits intopacedotdev:masterfrom
candidosales:fix-bug
Open

[BUG] Does not return the last item in the batch list#2
candidosales wants to merge 2 commits intopacedotdev:masterfrom
candidosales:fix-bug

Conversation

@candidosales
Copy link

@candidosales candidosales commented Jul 7, 2020

The error occurs when:

  • You pass just one item and it returns null or
  • When you list elements and never stop at the last item.

Example: https://play.golang.org/p/R8TgDSz40Ow

Example corrected: https://play.golang.org/p/y7eBkdUhTDj

New implementation:

func batch(count, batchSize int, eachFn BatchFunc) error {
	for i := 0; i < count; i += batchSize {
		j := i + batchSize
		if j > count {
			j = count
		}
		err := eachFn(i, j)
		if err == errors.New("done") {
			return nil
		}
		if err != nil {
			return err
		}
	}
	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant