JerryI / Mathematica-ThreeJS-graphics-engine

A parser for Wolfram Mathematica's Graphics3D functions (written in JS)
29 stars 2 forks source link

this is a discussion, not an issue :) #12

Closed asukaminato0721 closed 3 months ago

asukaminato0721 commented 1 year ago

this parser is much like https://sourceacademy.org/sicpjs/4.1.1 .

(I am a fan of this topic (functional programming), by the way. I have completed the sicp-py, cs61a)

For clarity, evaluate has been implemented as a case analysis using conditional expressions. The disadvantage of this is that our function handles only a few distinguishable types of statements and expressions, and no new ones can be defined without editing the declaration of evaluate. In most interpreter implementations, dispatching on the type of a component is done in a data-directed style. This allows a user to add new types of components that evaluate can distinguish, without modifying the declaration of evaluate itself.

core[this.name](args, env)

so it's needless to write a huge switch case, and core.NewMethod just works.

sicp is one of my favourite book :)

JerryI commented 1 year ago

Hah, amazing! I didn't know about this :D As always, everything was done before, we just forgot or did not know

I think I came across with that on some stackexchange topics, where the guy asked how to call a function by a string typed by a user.

Btw, I think in such case scenario it is good to keep it simple in this way, that on some pages/or projects the functions can be extended by simple

core.Something = function(arg, env) {

}

therefore, it can fully integrate Wolfram Language with a static or dynamic web page via websockets, so one can say symbolic computations meet web-design (I did it in one of the side projects, which is basically a multipurpose web application working in the exact same fashion).

Mb the fact that somewhere var core is defined and cannot be redefined by a user makes me feel, there must a better solution like

new evaluator core(new web socket(), or some handler);

core.function1Name = function(arg, env) {

}

//or if it is a module

import {graphics3d} from @graphics3d;
import {graphics2d} from @graphics2d;
graphics3d.extend(core);
graphics2d.extend(core);

but since I am dumb in modern JS, I am afraid to step in to this territory ;)

asukaminato0721 commented 1 year ago

actually, by using ESM, this is very easy.

a.js

export const f = (a) => a+1;
export function g(b) {
  console.log(b);
}

just export them, needless to interact with core.

b.js

import * as a from "./a.js"
console.log(a['f'](1), a.f(1));
a['g'](2);
a.g(2);

put them in the same folder, and run node b.js, it just works. :)

by the way, try vite , it's very nice. (and support ESM out of the box). node currently need use .mjs or set "type" : "module" in package.json

if a.ts and b.ts or with .d.ts, we can even have the type hint :P.

asukaminato0721 commented 1 year ago

but it's not good for this project. since a need function in b, b need functions in a.

so just core.NewFunction is ok :P

JerryI commented 1 year ago

but it's not good for this project. since a need function in b, b need functions in a.

so just core.NewFunction is ok :P

Well, such a pity actually. I hope it will not introduce any troubles in future.

JerryI commented 1 year ago

regarding vite, it would be so so cool, if it could pull necessary modules when, for example core.Graphics3D was called, and then, the module was about to downloaded.

JerryI commented 1 year ago

Offtopic. Came across Observable, jeez it looks so great. UI and data visualization are great, but you can't edit the output cells... eh