Given functions multiply()
and convert()
(minimal examples of the concept, I'm actually planning to query a remote server inside each function), what is a shorter way to implement multiplyAndConvert()
?
// call two async functions passing the result the from the first
// as the argument to the second
multiplyAndConvert(3).startWithResult { val in
print("\(val.value!)")
}
func convert(_ val: Int) -> SignalProducer<String, NSError> {
return SignalProducer<String, NSError> { observer, _ in
observer.send(value: String(val))
observer.sendCompleted()
}
}
func multiply(_ val: Int) -> SignalProducer<Int, NSError> {
return SignalProducer<Int,NSError> { observer, _ in
observer.send(value: val * 2)
observer.sendCompleted()
}
}
func multiplyAndConvert(_ val: Int) -> SignalProducer<String, NSError> {
return SignalProducer<String, NSError> { observer, _ in
multiply(val).startWithResult { res2 in
switch res2 {
case .success(let val2):
convert(val2).startWithResult { res3 in
switch res3 {
case .success(let val3):
observer.send(value:val3)
observer.sendCompleted()
case .failure(let err):
observer.send(error:err)
}
}
case .failure(let err):
observer.send(error: err)
}
observer.sendCompleted()
}
}
}
I know there has to be a more elegant way to do this, but I'm stumped as to what it is. I've played around with map, flatMap, flatten, and most other functions but can't find a better solution.