Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// [1, 2, 3, 4, 5] -> [[1,2], [2,3], [3,4], [4,5]]
- public func makeClosePairs<C: Collection>(c: C) -> [C.SubSequence] {
- var out: [C.SubSequence] = []
- var index = c.startIndex
- while index < c.endIndex {
- let first = index
- let second = c.index(after: first)
- guard second < c.endIndex else { break }
- let slice = c[first...second]
- out.append(slice)
- index = second
- }
- return out
- }
- /// [1, 2, 3, 4, 5, 6] -> [1, 2, 3] [4, 5, 6]
- func sliceOnTwo<D: Collection>(collection: D) -> (left: D.SubSequence, right: D.SubSequence)
- where D.Index: Strideable, D.Index.Stride: SignedInteger
- {
- let distance = collection.startIndex.distance(to: collection.endIndex)
- let middlecount = distance / 2
- let middle = collection.startIndex.advanced(by: middlecount)
- let firsthalf = collection[...middle]
- let secondhalf = collection[middle...]
- return (left: firsthalf, right: secondhalf)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement