You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Guides/AdjacentPairs.md
+22-11Lines changed: 22 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,8 @@
5
5
6
6
Lazily iterates over tuples of adjacent elements.
7
7
8
-
This operation is available for any sequence by calling the `adjacentPairs()` method.
8
+
This operation is available for any sequence by calling the `adjacentPairs()`
9
+
method.
9
10
10
11
```swift
11
12
let numbers = (1...5)
@@ -15,38 +16,48 @@ let pairs = numbers.adjacentPairs()
15
16
16
17
## Detailed Design
17
18
18
-
The `adjacentPairs()` method is declared as a `Sequence` extension returning `AdjacentPairsSequence` and as a `Collection` extension returning `AdjacentPairsCollection`.
19
+
The `adjacentPairs()` method is declared as a `Sequence` extension returning
20
+
`AdjacentPairsSequence` and as a `Collection` extension returning
The `AdjacentPairsSequence` type is a sequence, and the `AdjacentPairsCollection` type is a collection with conditional conformance to `BidirectionalCollection` and `RandomAccessCollection` when the underlying collection conforms.
35
+
The `AdjacentPairsSequence` type is a sequence, and the
36
+
`AdjacentPairsCollection` type is a collection with conditional conformance to
37
+
`BidirectionalCollection` and `RandomAccessCollection` when the underlying
38
+
collection conforms.
33
39
34
40
### Complexity
35
41
36
42
Calling `adjacentPairs` is an O(1) operation.
37
43
38
44
### Naming
39
45
40
-
This method is named for clarity while remaining agnostic to any particular domain of programming. In natural language processing, this operation is akin to computing a list of bigrams; however, this algorithm is not specific to this use case.
This method is named for clarity while remaining agnostic to any particular
47
+
domain of programming. In natural language processing, this operation is akin to
48
+
computing a list of bigrams; however, this algorithm is not specific to this use
49
+
case.
43
50
44
51
### Comparison with other languages
45
52
46
-
This function is often written as a `zip` of a sequence together with itself, minus its first element.
53
+
This function is often written as a `zip` of a sequence together with itself,
54
+
minus its first element.
47
55
48
56
**Haskell:** This operation is spelled ``s `zip` tail s``.
49
57
50
-
**Python:** Python users may write `zip(s, s[1:])` for a list with at least one element. For natural language processing, the `nltk` package offers a `bigrams` function akin to this method.
58
+
**Python:** Python users may write `zip(s, s[1:])` for a list with at least one
59
+
element. For natural language processing, the `nltk` package offers a `bigrams`
60
+
function akin to this method.
51
61
52
-
Note that in Swift, the spelling `zip(s, s.dropFirst())` is undefined behavior for a single-pass sequence `s`.
62
+
Note that in Swift, the spelling `zip(s, s.dropFirst())` is undefined behavior
The `ChunkedBy` and `ChunkedOn` types are bidirectional when the wrapped collection is
90
-
bidirectional.
89
+
The `ChunkedByCollection` and `ChunkedOnCollection` types are bidirectional when
90
+
the wrapped collection is bidirectional.
91
91
92
92
### Complexity
93
93
94
94
The eager methods are O(_n_), the lazy methods are O(_1_).
95
95
96
96
### Naming
97
97
98
-
The operation performed by these methods is similar to other ways of breaking a collection up into subsequences. In particular, the predicate-based `split(where:)` method looks similar to `chunked(on:)`. You can draw a distinction between these different operations based on the resulting subsequences:
99
-
100
-
-`split`: *In the standard library.* Breaks a collection into subsequences, removing any elements that are considered "separators". The original collection cannot be recovered from the result of splitting.
101
-
-`chunked`: *In this package.* Breaks a collection into subsequences, preserving each element in its initial ordering. Joining the resulting subsequences re-forms the original collection.
102
-
-`sliced`: *Not included in this package or the stdlib.* Breaks a collection into potentially overlapping subsequences.
103
-
98
+
The operation performed by these methods is similar to other ways of breaking a
99
+
collection up into subsequences. In particular, the predicate-based
100
+
`split(where:)` method looks similar to `chunked(on:)`. You can draw a
101
+
distinction between these different operations based on the resulting
102
+
subsequences:
103
+
104
+
-`split`: *In the standard library.* Breaks a collection into subsequences,
105
+
removing any elements that are considered "separators". The original collection
106
+
cannot be recovered from the result of splitting.
107
+
-`chunked`: *In this package.* Breaks a collection into subsequences,
108
+
preserving each element in its initial ordering. Joining the resulting
109
+
subsequences re-forms the original collection.
110
+
-`sliced`: *Not included in this package or the stdlib.* Breaks a collection
0 commit comments