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
| struct QueueFullError<T>: Error { | |
| let value: T | |
| } | |
| struct Queue<T> { | |
| private var elements: [T] = [] | |
| var first: T? { elements.first } | |
| var last: T? { elements.last } | |
| var isEmpty: Bool { elements.isEmpty } |
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
| /* | |
| Ref: | |
| - https://gist.github.com/rjchatfield/72629b22fa915f72bfddd96a96c541eb | |
| - https://www.hackingwithswift.com/swift/5.4/result-builders | |
| - https://www.avanderlee.com/swift/result-builders | |
| - https://www.swiftbysundell.com/articles/deep-dive-into-swift-function-builders | |
| - https://medium.com/@carson.katri/create-your-first-function-builder-in-5-minutes-b4a717390671 | |
| */ | |
| @resultBuilder | |
| struct ArrayBuilder<Element> { |
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
| /* | |
| Refs: | |
| - https://swiftrocks.com/understanding-opaque-return-types-in-swift | |
| - https://www.avanderlee.com/swift/some-opaque-types | |
| - https://www.avanderlee.com/swift/existential-any | |
| - https://swiftrocks.com/whats-any-understanding-type-erasure-in-swift | |
| */ | |
| protocol ImageFetching<Image> { | |
| associatedtype Image |
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 Foundation | |
| let queue = DispatchQueue(label: "my.queue", attributes: .concurrent) | |
| print("A") | |
| queue.async { // Task1 | |
| for index in 0 ..< 5 { | |
| sleep(2) | |
| print("Task 1 - async \(index)") | |
| } |
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
| // Ref: https://www.linkedin.com/posts/ihendi_ios-interview-questions-answers-could-activity-7094859984340951040-iL-x/ | |
| struct PersonNameEmpty: Error {} | |
| struct Person { | |
| var name: String = "" | |
| var formattedName: String { | |
| get throws { | |
| guard name.isEmpty == false else { throw PersonNameEmpty() } |
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
| class Test { | |
| private var _name: String = "Hello, World!" | |
| let queue = DispatchQueue(label: "thread.safe", attributes: .concurrent) | |
| var name: String { | |
| get { | |
| queue.sync { | |
| self._name | |
| } |
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
| // Ref: https://www.avanderlee.com/swift/actors | |
| actor ChickenFeeder { | |
| let food = "worms" | |
| var numberOfEatingChickens: Int = 0 | |
| func chickenStartsEating() { | |
| numberOfEatingChickens += 1 | |
| } |
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
| /* | |
| Ref: | |
| - https://springframework.guru/gang-of-four-design-patterns/strategy-pattern | |
| - https://refactoring.guru/design-patterns/strategy/swift/example | |
| */ | |
| import Foundation | |
| protocol MessageEncryptor { | |
| func encrypt(_ value: String) -> String |
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
| /* | |
| Ref: | |
| - https://springframework.guru/gang-of-four-design-patterns/decorator-pattern | |
| - https://refactoring.guru/design-patterns/decorator/swift/example | |
| */ | |
| import Foundation | |
| enum DataProviderError: Error { | |
| case unavailable |
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
| // Ref: https://www.youtube.com/watch?v=V_T5NuccwRA | |
| func find(_ compare: Int, in array: [Int]) -> Int? { | |
| var low = 0 | |
| var high = array.count - 1 | |
| repeat { | |
| let mid = (low + high)/2 | |
| let current = array[mid] | |
| // print(low...high, mid, current, compare) |