Kevin
Kevin

Reputation: 3150

Swift closures in Typescript/Javascript

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?

Upvotes: 0

Views: 326

Answers (1)

Artem
Artem

Reputation: 1870

I think your question is about Associated Values, not about Closures by themselves.

TypeScript has not full analogue of associated values, but you can do something like that:

const enum Operation {
  preFixUnaryOperation,
  postFixUnaryOperation,
  binaryOperation
}

type IOperation = {
  type: Operation.preFixUnaryOperation | Operation.postFixUnaryOperation;
  func: (a: number) => number;
} | {
  type: Operation.binaryOperation;
  func: (a: number, b: number) => number;
}

let operations: {[key: string]: IOperation} = {
  '+': { type: Operation.binaryOperation, func: (a, b) => a + b },
  'x²': { type: Operation.postFixUnaryOperation, func: (a) => a * a }
};

let operation = operations[someKey];

switch (operation.type) {
  case Operation.binaryOperation:
    return operation.func(left, right);
  case Operation.preFixUnaryOperation:
    return operation.func(right);      
  case Operation.postFixUnaryOperation:
    return operation.func(left);
}

See also: Discriminated Unions

Upvotes: 1

Related Questions