Created
October 11, 2022 22:04
-
-
Save 0xLeif/689d5f8ebab4c7e3b5bc530304bd459a to your computer and use it in GitHub Desktop.
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
// MARK: Power of Extensions | |
// [Swift Book](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html) | |
// MARK: - [Extension Syntax](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html#ID470) | |
extension Bool { | |
} | |
// MARK: - [Computed Properties](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html#ID152) | |
extension Bool { | |
var isFalse: Bool { self == false } | |
} | |
extension Array { | |
var isNotEmpty: Bool { first != nil } | |
} | |
// MARK: - [Initializers](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html#ID153) | |
extension Bool { | |
init(value: Int, equals otherValue: Int) { | |
self = value == otherValue | |
} | |
} | |
// MARK: - [Methods](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html#ID154) | |
extension Bool { | |
mutating func flip() { | |
self = .random() | |
} | |
} | |
// MARK: - [Subscripts](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html#ID156) | |
extension Array { | |
subscript(filter: (Element) -> Bool) -> Self { | |
self.filter(filter) | |
} | |
} | |
let filtered = Array([1, 2, 3])[ | |
{ value in | |
value.isMultiple(of: 2) | |
} | |
] | |
// MARK: - [Nested Types](https://docs.swift.org/swift-book/LanguageGuide/Extensions.html#ID157) | |
extension Bool { | |
indirect enum Token { | |
case equalTo, lessThan, greaterThan | |
case or(Token, Token), and(Token, Token) | |
func or(_ other: Token) -> Token { | |
.or(self, other) | |
} | |
func and(_ other: Token) -> Token { | |
.and(self, other) | |
} | |
func evaluate<Value: Comparable>( | |
value: Value, | |
otherValue: Value | |
) -> Bool { | |
switch self { | |
case .equalTo: return value == otherValue | |
case .lessThan: return value < otherValue | |
case .greaterThan: return value > otherValue | |
case let .or(lhs, rhs): | |
return lhs.evaluate(value: value, otherValue: otherValue) || | |
rhs.evaluate(value: value, otherValue: otherValue) | |
case let .and(lhs, rhs): | |
return lhs.evaluate(value: value, otherValue: otherValue) && | |
rhs.evaluate(value: value, otherValue: otherValue) | |
} | |
} | |
} | |
init(value: Int, is token: Token, _ otherValue: Int) { | |
self = token.evaluate(value: value, otherValue: otherValue) | |
} | |
} | |
// MARK: - [Adding Protocol Conformance with an Extension](https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#ID277) | |
import SwiftUI | |
struct CoolView: View { | |
var body: some View { | |
Text("๐") | |
// ... | |
"๐" | |
} | |
} | |
extension String: View { | |
public var body: some View { Text(self) } | |
} | |
// MARK: - Generics and the Where Clause | |
// [Extending a Generic Type](https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID185) | |
// [Extensions with a Generic Where Clause](https://docs.swift.org/swift-book/LanguageGuide/Generics.html#ID553) | |
extension Array where Element: Equatable { | |
var isEqual: Bool { | |
reduce(into: Self()) { partialResult, element in | |
guard partialResult.contains(element).isFalse else { | |
return | |
} | |
partialResult.append(element) | |
} | |
.count <= 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment