-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add adjacent and neighbors functions
- Loading branch information
1 parent
b2fe649
commit afa917a
Showing
2 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Jacob Hearst on 10/31/21. | ||
// | ||
|
||
import Foundation | ||
|
||
extension StringProtocol { | ||
func distance(of element: Element) -> Int? { firstIndex(of: element)?.distance(in: self) } | ||
func distance<S: StringProtocol>(of string: S) -> Int? { range(of: string)?.lowerBound.distance(in: self) } | ||
|
||
subscript(offset: Int) -> Character { | ||
self[index(startIndex, offsetBy: offset)] | ||
} | ||
} | ||
|
||
extension Collection { | ||
func distance(to index: Index) -> Int { distance(from: startIndex, to: index) } | ||
} | ||
|
||
extension String.Index { | ||
func distance<S: StringProtocol>(in string: S) -> Int { string.distance(to: self) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters