Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What features and functionalities is AskScript missing? #402

Open
czerwinskilukasz1 opened this issue Jul 17, 2020 · 4 comments
Open

What features and functionalities is AskScript missing? #402

czerwinskilukasz1 opened this issue Jul 17, 2020 · 4 comments
Labels
AskScript ./src/askscript/** AskVM ./src/askvm/** discussion A discussion is taking place, please don't work on it yet enhancement New feature or request P2 type:resources related to AskVM resources type:syntax related to syntax or syntax sugar

Comments

@czerwinskilukasz1
Copy link
Collaborator

I tried to translate a couple of Javascript scripts to AskScript and often I get blocked on some missing resources or language constructs.
This ticket will list them all.

@czerwinskilukasz1 czerwinskilukasz1 added enhancement New feature or request AskScript ./src/askscript/** AskVM ./src/askvm/** discussion A discussion is taking place, please don't work on it yet type:syntax related to syntax or syntax sugar type:resources related to AskVM resources labels Jul 17, 2020
@czerwinskilukasz1
Copy link
Collaborator Author

czerwinskilukasz1 commented Jul 17, 2020

Example 1.

I wanted to translate the following program from Javascript to AskScript and got blocked.

Javascript:

const sum_pairs = function (ints, s) {
  const leftMostPosition = {};
  for (let i = 0; i < ints.length; ++i) {
    const a = ints[i];
    if (!(a in leftMostPosition)) {
      leftMostPosition[a] = i;
    }
  }
  
  for (let i = 0; i < ints.length; ++i) {
    const b = ints[i];
    const a = s - b;
    if (a in leftMostPosition && leftMostPosition[a] < i) {
      return [a, b];
    }
  }
};

I bumped into the following problems:

The current status of the AskScript code is:

// Below is a complete solution for a 5kyu Codewars task: 
// https://www.codewars.com/kata/54d81488b981293527000c8f
ask {
  const sumPairs = fun (ints:array(int), s:int) {
    let leftMostPosition: map(int) = {};
    for (const i = 0; i < ints.length; i=i+1) {
      const a = ints[i];
      if (leftMostPosition[a] == null) {
        leftMostPosition = set(leftMostPosition, a, i);
      }
    }
    
    for (const i = 0; i < ints.length; ++i) {
      const b = ints[i];
      const a = s - b;
      if ((leftMostPosition[a] != null) && (leftMostPosition[a] < i)) {
        return [a, b];
      }
    }
  }

  [
    sumPairs([11, 3, 7, 5],         10)  == [3, 7],
    sumPairs([4, 3, 2, 3, 4],        6)  == [4, 2],
    sumPairs([0, 0, -2, 3], 2)           == null  ,
    sumPairs([10, 5, 2, 3, 7, 5],   10)  == [3, 7]
  ]
}

@mhagmajer
Copy link
Contributor

Very interesting experiment, @czerwinskilukasz1! Couple points:

  • why not use forOf here for iterating arrays?
  • {} is a different type in JavaScript than a map, I'm not sure if we need to make the same distinction
  • if we make it so that for returns a value your program will need to have explicit return empty at the end

What can be done to improve the simplicity and readability of your solution when coded in AskScript. What are the parts that you like and dislike?

@czerwinskilukasz1
Copy link
Collaborator Author

Very interesting experiment, @czerwinskilukasz1! Couple points:

  • why not use forOf here for iterating arrays?

Simply I wanted to change as little code as possible.

  • {} is a different type in JavaScript than a map, I'm not sure if we need to make the same distinction

Currently the only associative type in AskScript is map. There is no object, record or any other similar type.

  • if we make it so that for returns a value your program will need to have explicit return empty at the end

Good point. The tests will catch it.

What can be done to improve the simplicity and readability of your solution when coded in AskScript. What are the parts that you like and dislike?

Two things I would improve, based on this program:

  1. I needed to change ++i to i=i+1, which was a bit disturbing.
  2. The need to write brackets that separate different operators in if ((leftMostPosition[a] != null) && (leftMostPosition[a] < i)) { disturbed me too.

@czerwinskilukasz1
Copy link
Collaborator Author

czerwinskilukasz1 commented Jul 17, 2020

Example 2.

I wanted to translate the following Javascript example to AskScript and needed to write more lines of code when using cache.
(Also, hasKey would be useful, but I mentioned it already in Example 1, and push would be nice to have too)

Javascript:

var fibonacci = function(n, cache) {
    if (typeof cache === 'undefined') {
      cache = [0, 1];
    }
    
    if(n==0 || n == 1)
        return n;
    
    if(n < cache.length)
      return cache[n];
    
    const result = fibonacci(n-1, cache) + fibonacci(n-2, cache);
    cache.push(result);
    return result;
}

AskScript:

ask {
    const fibonacci = fun(n:int, cache:array(int)) {
        if (cache:length == 0) {
          cache = [0, 1];
        }
        
        if ((n==0) || (n == 1)) {
            return [n, cache];
        }
        
        if(n < cache:length) {
          return [cache[n], cache];
        }
        
// === Here is where more lines are needed because all variables in AskScript are immutable.

        const res1 = fibonacci(n-1, cache);
        cache = res1[1];

        const res2 = fibonacci(n-2, cache);
        cache = res2[1];

        const result = res1[0] + res2[0];
        cache = cache:set(n, result);
        return [result, cache];
// === ===
    }
    
    [
        fibonacci(0, [])[0],
        fibonacci(1, [])[0],
        fibonacci(2, [])[0],
        fibonacci(3, [])[0],
        fibonacci(4, [])[0],
        fibonacci(5, [])[0],
        fibonacci(6, [])[0]
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AskScript ./src/askscript/** AskVM ./src/askvm/** discussion A discussion is taking place, please don't work on it yet enhancement New feature or request P2 type:resources related to AskVM resources type:syntax related to syntax or syntax sugar
Projects
None yet
Development

No branches or pull requests

2 participants