Package Management
lidangzzz opened this issue · 0 comments
Is your feature request related to a problem? Please describe.
Yes we absolutely need a way to allow user to import codes/modules/libraries via package management.
Describe the solution you'd like
1. Allow user to use import(LIB_URL) to import library
let tf = import("https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js");
const a = tf.tensor([[1, 2], [3, 4]]);
2. Allow user to fetch a hedgehog script/library from url, including
- Fetch the raw string
- Cache to window.localStorage if necessary (item key: url, value: the raw string of hedgehog script file),
For example, the library "linearRegression" code is hosted by Github Gist at https://gist.githubusercontent.com/lidangzzz/a14456264724a92b5a1147036537e531/raw/e0bb0e1f5f763ca076ef726be73e3bcc98f048bd/linearRegression.hhs
class LinearRegression {
w: Mat;
constructor() { }
//x: M-by-N matrix. M data with N dimensions. Each row is an N-dim vector
//y: M-by-1 matrix
fit(x_: Mat, y_: Mat) : LinearRegression{
let y = y_;
if (y_.rows != 1 && y_.cols == 1) {y = y_.T();} //check the dimension of y
var x = x_.resize(x_.rows, x_.cols + 1, 1); //expan x_ with one more column with 1
this.w = ( (X.T() * X)^-1 ) * X.T() * y //calculate w = (X.T() * X)^-1 * X.T() * y
return this;
}
//x: M-by-N matrix. M data with N dimensions. Each row is an N-dim vector
predict(x_: Mat):mat {
let x = x_.resize(x_.rows, x_.cols + 1, 1); //expan x_ with one more column with 1
return x*(this.w);
}
}
and user can import and use the library in this way:
#include https://gist.githubusercontent.com/lidangzzz/a14456264724a92b5a1147036537e531/raw/e0bb0e1f5f763ca076ef726be73e3bcc98f048bd/linearRegression.hhs
let training = readCSV('http://kaggle.com/dataset_training.csv');
let testingX = readCSV('http://kaggle.com/dataset_testing.csv');
let predicted_Y = new LinearRegression().fit(training.X, training.Y).predict(testingX);
print(predicted_Y)
Users can temporally or permanently save their libraries on github gist, or github repo, or build their own code management server on their own hand.
Notes:
-
The differences between (1) and (2) is that the babel compiler should also compile the hedgehog script in (2) with user's input code together before executing, but not for the common JavaScript module in (1). So it's necessary to split dependencies (1) and (2), compile (2) with user's code first, then add "import" part of (1) before execution;
-
A straight-forward idea for implementing (2) is just concatenating all raw strings of libraries into a single chunk of code before compiling. For example:
Lib1.hss
let pi = 3.1415926;
function foo(A){
return A*pi + sin(A*pi);
}
Lib2.hss
#include https://myLib.com/Lib1.hss
function bar(A){
return foo(A) * A^-1
}
user's input code
#include https://another-website.com/Lib2.hss
print(bar(zeros(1,100))
And after fetching the libraries by traversing the dependency tree and concatenating all scripts, the code for compiling is
let pi = 3.1415926;
function foo(A){
return A*pi + sin(A*pi);
}
function bar(A){
return foo(A) * A^-1
}
print(bar(range(1,100).reshape(10,1)))
Describe alternatives you've considered
No
Additional context
No