I am trying to convert the Swift calculator created by Paul Heggarty for the course CS193p (Stanford, Winter 2017) to Typescript code (Ionic 3 (Angular 4) in this case), to see the differences.
I'm not focussing on doing as much as possible the same way.
I've come pretty far, but I'm struggeling with doing the following in typescript.
private var operations: Dictionary<String,Operation> = [
"+" : Operation.binaryOperation({ $0 + $1 }),
"−" : Operation.binaryOperation({ $0 - $1 }),
"÷" : Operation.binaryOperation({ $0 / $1 }),
"×" : Operation.binaryOperation({ $0 * $1 }),
"π" : Operation.constant(Double.pi),
"√" : Operation.preFixUnaryOperation(sqrt),
"x²" : Operation.postFixUnaryOperation({ pow($0, 2) }),
"x³" : Operation.postFixUnaryOperation({ pow($0, 3) }),
"=" : Operation.equals,
"±" : Operation.preFixUnaryOperation({ -$0 }),
"sin": Operation.preFixUnaryOperation(sin),
"cos": Operation.preFixUnaryOperation(cos),
"tan": Operation.preFixUnaryOperation(tan),
"e" : Operation.constant(M_E)
]
What I did so far, was creating a private variable, called "operations" with type "{}".
Then "fill" it by calling:
this.operations['+' = Operation.binaryOperation(() => {arguments[0] + argumenst[1]})
The filling part does work. Except I can't figure out how to parse functions like this.
enum Operation in Swift:
private enum Operation {
case constant(Double)
case preFixUnaryOperation((Double) -> Double)
case postFixUnaryOperation((Double) -> Double)
case binaryOperation((Double,Double) -> Double)
case equals
}
enum Operation in Typescript (same problem):
export enum Operation {
constant, // Double is an assosiative value, its like the assosiated value in the 'set' state of an optional.
preFixUnaryOperation,
postFixUnaryOperation,
binaryOperation,
equals
}
How should I use a, which is called in Swift, "closure", in typescript/javascript?