-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement loops like for, while, until as functions #2184
Comments
That's cool. Thank you! |
I think we can implement multi branch structure and loop structure based on function, just like the examples I wrote above. {
if: typed('if', {
'Matrix': function (mat: Matrix) {
let shape = mat.size();
if (shape.length < 2 || shape[1] < 2) {
throw new Error('The shape of the matrix is incorrect');
}
let arr = mat.toArray();
return import_scope.if(arr);
},
'Array': function (arr: Array<[any, any]>) {
for (let p of arr) {
if ((p[0] && p[0].evaluate && p[0].evaluate(that.scope)) ||
(p[0] && !p[0].evaluate)) {
return (p[1] && p[1].evaluate && p[1].evaluate(that.scope)) || p[1];
}
}
}
}),
while: function (nodes: MathNode[], _math: Partial<math.MathJsStatic>, _scope: object) {
const [c, p, r] = nodes;
const _c = c && c.compile();
const _p = p && p.compile();
while (_c && _c.evaluate(that.scope)) {
_p && _p.evaluate(that.scope);
}
return r && r.evaluate(that.scope);
},
until: function (nodes: MathNode[], _math: Partial<math.MathJsStatic>, _scope: object) {
const [c, p, r] = nodes;
const _c = c && c.compile();
const _p = p && p.compile();
_p && _p.evaluate(that.scope);
while (!(_c && _c.evaluate(that.scope))) {
_p && _p.evaluate(that.scope);
}
return r && r.evaluate(that.scope);
},
for: function (nodes: MathNode[], _math: Partial<math.MathJsStatic>, _scope: object) {
const [s, c, e, p, r] = nodes;
const _c = c && c.compile();
const _p = p && p.compile();
const _e = e && e.compile();
s && s.evaluate(that.scope);
while (_c && _c.evaluate(that.scope)) {
_p && _p.evaluate(that.scope);
_e && _e.evaluate(that.scope);
}
return r && r.evaluate(that.scope);
}
} import_scope.while['rawArgs'] = true;
import_scope.until['rawArgs'] = true;
import_scope.for['rawArgs'] = true; |
The advantage of this implementation is that there is no need to modify the parser significantly. Maybe it's cool to put the code in the matrix and then be executed! |
Ahh, now I see what you're doing: implementing loops as functions. That's a smart idea! I have to give this some more thought, for short term this is a great solution, but I'm not sure if it will bite us in the future: if we go this direction now, I think it will not be easy to switch to "real" for/while/until operators later on. |
I've been thinking about this a bit more. I do like your simple, pragmatic solution, but I think we should not go for this in the long run. Implementing those loops in the expression parser for real (#518) should not be that complicated, I think it is better to go for the long term solution right away to keep things straightforward for the user. Users that are in bad need for this can of course use your solution as a workaround 😄 . |
Yes, you're right, we do need a long-term solution 😆 |
Ok 👍 , will close this issue now, let's continue any discussion in #518. |
I recently used mathjs to build a programming language for mathematical operations.
The language is like this.
The text was updated successfully, but these errors were encountered: