This repository was archived by the owner on Dec 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Sequences
DimaMishchenko edited this page Jul 17, 2018
·
2 revisions
An EasyFutures provides some functions to help you work with the sequences of Futures.
You can convert a list of the values into a single value. Fold returns the Future with this value. Fold takes default value and then you perform action with default value and every value from the list.
let futures = [Future<Int>(value: 1), Future<Int>(value: 2), Future<Int>(value: 3)]
futures.fold(0) { defaultValue, currentValue -> Int in
return defaultValue + currentValue
}.onSuccess { value in
print(value) // 6
}Can catch errors.
let error = NSError(domain: "", code: -1, userInfo: nil)
futures.fold(0) { defaultValue, currentValue -> Int in
throw error
}.onError { error in
print(error) // error
}Traverse can work with any sequence. Takes closure where you transform the value into the Future. Returns the Future which contains array of the values from the Futures returned by the closure.
[1, 2, 3].traverse { number -> Future<String> in
return Future<String>(value: "\(number * 100)")
}.onSuccess { value in
print(value) // ["100", "200", "300"]
}Transforms a list of the Futures into the single Future with an array of values.
let futures = [Future<Int>(value: 1), Future<Int>(value: 2), Future<Int>(value: 3)] // [Future<Int>, Future<Int>, Future<Int>]
let sequence = futures.sequence() // Future<[Int]>
sequence.onSuccess { numbers in
print(numbers) // [1, 2, 3]
}