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
let timeout = after(seconds: 4) | |
race(when(fulfilled: fetch(url:url)).asVoid(), timeout).then { | |
//… | |
} |
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
func attempt<T>(maximumRetryCount: Int = 3, delayBeforeRetry: DispatchTimeInterval = .seconds(2), _ body: @escaping () -> Promise<T>) -> Promise<T> { | |
var attempts = 0 | |
func attempt() -> Promise<T> { | |
attempts += 1 | |
return body().recover { error -> Promise<T> in | |
guard attempts < maximumRetryCount else { throw error } | |
return after(delayBeforeRetry).then(on: nil, attempt) | |
} | |
} | |
return attempt() |
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
extension Foo { | |
static func promise() -> Promise<FooResult> { | |
return PromiseWithDelegate().promise | |
} | |
} | |
class PromiseWithDelegate: FooDelegate { | |
let (promise, seal) = Promise<FooResult>.pending() | |
private let foo = Foo() |
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
login().then { username in | |
getTokens(for: username).map { ($0, username) } | |
}.then { tokens, username in | |
//… | |
} |
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
let p1 = promise1.then { | |
... | |
} | |
let p2 = promise2.then { | |
... | |
} | |
when(fulfilled: p1, p2).catch { error in | |
... |
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
func foo() -> (Promise<Void>, cancel: () -> Void) { | |
let task = Task(…) | |
var cancelme = false | |
let promise = Promise<Void> { seal in | |
task.completion = { value in | |
guard !cancelme else { reject(NSError.cancelledError) } | |
seal.fulfill(value) | |
} | |
task.start() |
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 ViewController: UIViewController { | |
private let (promise, seal) = Guarantee<...>.pending() | |
func show(in: UIViewController) -> Promise<…> { | |
in.show(self, sender: in) | |
return promise | |
} | |
func done() { | |
dismiss(animated: true) |
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
et number = UInt32(Calendar.current.component(.weekday, from: Date())) | |
let msg = ["D", "(", "o", "o", "#", "(", "w", "k", "h", "#", "e", "(", "h", "v", "w", "!", "(", "#", "d", "q", "(", "g", "#", "n", "h", "h", ")", "s", "#", "r", "!", "q", "#", "h", "y", "d", "!", "o", "x", "d", "w", "(", "l", "q", "j"] | |
print(msg.filter{$0.unicodeScalars.map{$0.value}.reduce(0, +) > 64 | |
|| $0.unicodeScalars.map{$0.value}.reduce(0, +) == 35}.map{ | |
Character(UnicodeScalar($0.unicodeScalars.map{$0.value}.reduce(0, +) | |
- number)!)}) |
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
func rot3(msg: String, op: (UInt32, UInt32) -> UInt32) -> String { | |
let numbers = msg.unicodeScalars.map{$0.value} | |
return String(numbers.map{Character(UnicodeScalar(op($0, 3))!)}) | |
} |
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
func rotFinal(msg: String, key: String, op: (UInt32, UInt32) -> UInt32) -> String { | |
return String(zip(msg.unicodeScalars.map{$0.value}, String(repeating: key, count: (msg.count / key.count) + 1) | |
.unicodeScalars.map{$0.value}) | |
.map{Character(UnicodeScalar(op($0.0, $0.1))!)}) | |
} |