Batch is not sound on `head` and other remarks
See original GitHub issueHi!
I stumbled by chance on the new Batch collection added recently in this library. After a quick look at the implementation, it exhibits a few weaknesses and at least a couple bugs. Using a single ticket to collect my thoughts on it, a PR might follow if I find the time.
Combine case is not guaranteed to have non-empty children, whereas head and headOption assume so. At the same time isEmpty indicates the implementation may allow non-empty children.
This test fails:
assert(Batch(Batch.Empty, Batch(1, 2, 3)).head == 1)
// java.util.NoSuchElementException: Batch.Empty.head
Two approaches:
- (a) ensure the children of
Combineare never empty by using the++everywhere. Can be error-prone but simple enough to implement, might be worth adding implementation comments on the design decision. Will make theisEmptyalways false. - (b) allow empty children and fix
headandheadOptionaccordingly.
The mkString throws an exception on the empty cases.
The mkString(" ") also adds a trailing separator in the case of single element, which is not expected?
Failing tests:
test("mkString") {
assert(Batch.Empty.mkString == "")
assert(Batch(1).mkString(" ") == "1")
}
The memoization in _jsArray is simple enough, but a bit arbitrary as some methods will use it, and some won’t. It could be interesting to try to leverage this memoization in the Wrapped and Combine cases, actually making sure it’s computed only once and short-circuiting all the methods on it. To simplify the Batch implementation it could also be an option to drop the Empty and Singleton (replaced with Wrapped), without any performance impact.
The catch { _ => false } fallback in equality check is not idiomatic and may have serious performance impact.
I am not sure on how js.Array is implemented, but the unapply on :: might have misleading performance, splitting the array over and over again in the tail?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (5 by maintainers)

Top Related StackOverflow Question
Hey thanks for the quick reply, Saturday is definitely for side projects 😄
Your listed goals make total sense for sure! And I will open dedicated issues and tasks if I find the time to work on them a bit more, it’s easier to show what I meant with some draft code.
Cheers
I believe I’ve answered most of this in the work that is now in
main, very helpful - thank you! Would be happy to re-open if you think I’ve missed anything or address individual issues on a case by case basis.