Вы можете использовать zip
для одновременной итерации по двум массивам, а затем map
для преобразования пар элементов в выходные данные, как вам бы этого хотелось.Полезно включить (ln.isNaN, tan.isNaN)
и использовать это для выражения различных случаев и их результатов.
Вот примерное начало:
import Darwin
func promptForDouble(
initialMessage: String,
errorMessage: String,
acceptanceCriteria isAcceptable: (Double) -> Bool = { _ in true }
) -> Double {
print(initialMessage)
while true {
if let number = readLine().flatMap(Double.init), isAcceptable(number) {
return number
}
print(errorMessage)
}
}
let initValue = promptForDouble(
initialMessage: "Enter the starting number of the range",
errorMessage: "Enter the correct number!"
)
let finalValue = promptForDouble(
initialMessage: "Enter the end value of the range",
errorMessage: "Enter the correct number, which is greater than starting number of the range!",
acceptanceCriteria: { initValue < $0 }
)
let stepValue = promptForDouble(
initialMessage: "Enter delta",
errorMessage: "Enter the correct number!"
)
// TODO: give these functions better names!
func calcLn(_ input: [Double]) -> [Double] {
return input.map { log(1 - 46/sin($0)) }
}
func calcTan(_ input: [Double]) -> [Double] {
return input.map { tan($0) / 46 }
}
func mergeResults(lns: [Double], tans: [Double]) -> [Double?] {
return zip(lns, tans).map { ln, tan -> Double? in
switch (ln.isNaN, tan.isNaN) {
case ( true, true): return nil // Return nil to express error. Don't introduce Strings yet.
case (false, true): return ln
case ( true, false): return tan
case (false, false): return max(ln, tan)
}
}
}
func printResults(_ a1: [Double], _ a2: [Double], _ a3: [Double?]) {
for (a, (b, c)) in zip(a1, zip(a2, a3)) {
let resultString = c.map(String.init) ?? "terrible error" // Note: Strings are only introduced at the UI/presentation layer
print("ln: \(a),\ttan: \(b),\tresult: \(resultString)")
}
}
// TODO: give these arrays better names!
let inputs = Array(stride(from: initValue, through: finalValue, by: stepValue))
let array1 = calcLn(inputs)
let array2 = calcTan(inputs)
let array3 = mergeResults(lns: array1, tans: array2)
printResults(array1, array2, array3)