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

Package Management #40

Open
lidangzzz opened this issue Jul 19, 2020 · 0 comments
Open

Package Management #40

lidangzzz opened this issue Jul 19, 2020 · 0 comments

Comments

@lidangzzz
Copy link
Member

lidangzzz commented Jul 19, 2020

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/[email protected]/dist/tf.min.js");
const a = tf.tensor([[1, 2], [3, 4]]);

2. Allow user to fetch a hedgehog script/library from url, including

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:

  1. 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;

  2. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant