# Add Various Zip-Related Types and Operations

**URL:** <https://forums.swift.org/t/add-various-zip-related-types-and-operations/13806>\
**Category:** Pitches\
**Created:** [June 18, 2018, 4:20pm UTC](https://forums.swift.org/t/add-various-zip-related-types-and-operations/13806 "2018-06-18T16:20:29Z")\
**Posts on this page:** 1\
**Showing post:** 46

<div class="post-metadata">

**Author:** ![dennisvennink](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dennisvennink/32/3637_2.png) [@dennisvennink](https://forums.swift.org/u/dennisvennink)\
**Post date:** [October 30, 2018, 3:49pm UTC](https://forums.swift.org/t/add-various-zip-related-types-and-operations/13806/46 "2018-10-30T15:49:26Z")

</div>

A preliminary implementation can be found [here](https://github.com/dennisvennink/Zip) (as a package).

Here's an overview of all available conformances on the most important types along with some preliminary motivation.

#### `Zip2Sequence / ZipLongest2Sequence`

| `:` | `where Sequence1 / 2:` |
| --- | --- |
| `Sequence` | `Sequence` |
| `LazySequenceProtocol` | `LazySequenceProtocol` |

Currently, when `Zip2Sequence`'s underlying `Sequence`s are lazy, their laziness gets lost. By conforming to `LazySequenceProtocol`, we get rid of this regression.

#### `Zip2Collection`

| `:` | `where Collection1 / 2:` |
| --- | --- |
| `Sequence` | `Sequence` |
| `LazySequenceProtocol`, `LazyCollectionProtocol` | `LazyCollectionProtocol` |
| `Collection` | `Collection` |
| `BidirectionalCollection` | `RandomAccessCollection` |
| `RandomAccessCollection` | `RandomAccessCollection` |
| `MutableCollection` | `MutableCollection` |
| `RangeReplaceableCollection` | `RangeReplaceableCollection` |
| `ExpressibleByArrayLiteral` | `RangeReplaceableCollection` |
| `ExpressibleByDictionaryLiteral` | `RangeReplaceableCollection` |

`Zip2Collection`'s `protocol` inheritance can roughly be split into four categories; support for: (1) laziness, (2) immutable and (3) mutable access and (4) literal initialisation.

1. The arguments made for `Zip2Sequence` also stand here.

2. The benefits of read-only access are obvious in terms of the extra performable operations and performance benefits. It also guarantees that it can be nondestructively consumed by iteration. Existing code might get performance benefits in certain unambigious situations as well "for free" since some of `Sequence`'s operations have more performant default implementations up the hierarchy (note to self: check if this truly is the case).

3. `Zip2Collection`'s conformance to `RangeReplaceableCollection` means `+(_:_:)` will become available for `zip`s.

4. The main reason for `ExpressibleByArrayLiteral` and `ExpressibleByDictionaryLiteral` conformance is to provide an alternative shorthand for creating a default value for when `Zip2Collection` is `Optional` or empty:

#### `ZipLongest2Collection`

| `:` | `where Collection1 / 2:` |
| --- | --- |
| `Sequence` | `Sequence` |
| `LazySequenceProtocol`, `LazyCollectionProtocol` | `LazyCollectionProtocol` |
| `Collection` | `Collection` |
| `BidirectionalCollection` | `RandomAccessCollection` |
| `RandomAccessCollection` | `RandomAccessCollection` |

`ZipLongest2Collection` is more limited than `Zip2Collection` as it can't guarantee `MutableCollection` and `RangeReplaceableCollection`'s behavioural semantics (as far as I can see). The arguments made in points 1 and 2 (partly) in `Zip2Collection` also stand here.

---

_[View the full topic](https://forums.swift.org/t/add-various-zip-related-types-and-operations/13806)._
