Affects Version/s: None
Fix Version/s: None
Component/s: Library (Misc)
Several collections in the Scala library have a #zip method. This method takes another collection and then merges it with the invocation target, producing a collection of type (A, B), where A is the component type of `this` and B is the component type of `that`.
However, I do not believe that this method is general enough. It somewhat-arbitrarily decides that the best way to "merge" to collection elements is to wrap them together in a 2-tuple. This is fine for correctness, but most often the user of the API must take an extra step to "unwrap" these values in order to produce the desired result. This can be inefficient, and generally constitutes extra code which can obscure intent.
I would request the addition of a #zipWith method (patterned after Haskell's function of the same name) to every collection which supports #zip. At a minimum, this method could be defined in terms of #zip and #map:
This is sub-optimal because it traverses the collection twice and produces an intermediary "throw-away" value, but it is the simplest approach.
This method would be useful in a number of situations, including (but not limited to) everyone's favorite infinite series (assuming :: syntax for Streams):
More generally though, I think it would be a more concise and more readable method in just about every situation where #zip would be applied. Logically, #zip is just a specialization of #zipWith, passing a function which takes two values and returns a tuple of the same.