Skip to content

Instantly share code, notes, and snippets.

@alistra
Created September 24, 2019 14:00
Show Gist options
  • Save alistra/171dea2db745b57e8cd2a1827ab704eb to your computer and use it in GitHub Desktop.
Save alistra/171dea2db745b57e8cd2a1827ab704eb to your computer and use it in GitHub Desktop.
extension Result: RandomAccessCollection, BidirectionalCollection, Collection, Sequence where Success: RandomAccessCollection, Success.Index == Int {
public typealias Element = Result<Success.Element, Failure>
public typealias Index = Int
public var startIndex: Int {
switch self {
case .success(let array):
return array.startIndex
case .failure:
return 0
}
}
public var endIndex: Int {
switch self {
case .success(let array):
return array.endIndex
case .failure:
return 1
}
}
public subscript(position: Int) -> Result<Success.Element, Failure> {
switch self {
case .success(let array):
return .success(array[position])
case .failure(let error):
return .failure(error)
}
}
}
print(“Hello World”)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment