Created
December 17, 2020 19:36
-
-
Save rogerluan/ee04febd80371f88f9435e98032b3042 to your computer and use it in GitHub Desktop.
Flattenable conforming entities define flattened, a function that flattens any arbitrary Optional, no matter how deeply nested, down to a single level of optionality.
This file contains hidden or 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
import XCTest | |
protocol Flattenable { | |
func flattened() -> Any? | |
} | |
extension Swift.Optional : Flattenable { | |
/// Flattens any arbitrary Optional, no matter how deeply nested, down to a single level of optionality. | |
/// - SeeAlso: Really fun discussion going on here: https://forums.swift.org/t/challenge-flattening-nested-optionals/24083 | |
/// - Returns: A single-level Optional. | |
func flattened() -> Any? { | |
switch self { | |
case .some(let x as Flattenable): return x.flattened() | |
case .some(let x): return x | |
case .none: return nil | |
} | |
} | |
} | |
final class FlattenableTests : XCTestCase { | |
func testFlattened() { | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Int>.none.flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Int>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Int>>.none?.flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Int>>.some(Optional<Int>.none).flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Int>>.some(Optional<Int>.some(1)).flattened(), b: 1)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Int>>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Int>>>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Int>>>>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Optional<Int>>>>>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Optional<Optional<Int>>>>>>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Optional<Optional<Optional<Int>>>>>>>.some(1).flattened(), b: Optional(1))) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Int>>>.none.flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Int>>>>.none.flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Optional<Int>>>>>.none.flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Optional<Optional<Int>>>>>>.none.flattened(), b: nil)) | |
XCTAssert(isEqual(type: Optional<Int>.self, a: Optional<Optional<Optional<Optional<Optional<Optional<Optional<Int>>>>>>>.none.flattened(), b: nil)) | |
let _20levelsNested: Int???????????????????? = 20 | |
XCTAssert(isEqual(type: Optional<Int>.self, a: _20levelsNested.flattened(), b: Optional(20))) | |
} | |
// MARK: - Utilities | |
/// Utility to compare `Any?` elements. | |
private func isEqual<T : Equatable>(type: T.Type, a: Any?, b: Any?) -> Bool { | |
guard let a = a as? T, let b = b as? T else { return false } | |
return a == b | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment