I am trying to learn Scala through making a Calculator. In this calculator, I have two methods in particular that do the parsing and computing. I also have two case classes that handle data.
The 'unique' data structure is just a composed of an operationIndex, which is just the two numbers and the operator to perform on them. I made a caseclass called numberPair that just holds the two numbers because I feel like it is easier to comprehend this way. Please correct me if I am wrong on this, and if i should just use a normal pair.
The scala compiler is complaining that the line
parser(data(element).numbers.number1, data(element).numbers.number2, data(element).operator)
returns a Calculator.OperationIndex. I don't get it, it's supposed to be returning a double as specified by the function signature. It's also saying that it requires an Int over a double. I've read the scala documentation, but i'm still not sure where I've went wrong.
case class NumberPair(number1: Double, number2: Double)
case class OperationIndex(numbers: NumberPair, operator: String)
def parser(number1: Double, number2: Double, operator: String): Double = {
val result = operator match {
case "+" => number1 + number2
case "-" => number1 - number2
case "/" => number1 / number2
case "*" => number1 * number2
}
result
}
def computeValue(data: List[OperationIndex]): Double = {
data.foldLeft(0.0) {
(acc: Double, element: OperationIndex) =>
acc + parser(data(element).numbers.number1, data(element).numbers.number2, data(element).operator)
}
}