Would it be a good idea to work on letting people add arrays of numbers in javascript?

I am a very mathematically oriented programmer, and I happen to be doing a lot of java script these days. I am really disappointed in the math aspects of javascript:

  • the Math object is almost a joke because it has so few methods
  • you can’t use ^ for exponentiation
  • the + operator is very limited, you cant add array’s of numbers or do scalar multiplication on arrays

Now I have written some pretty basic extensions to the Math object and have considered writing a library of advanced Math features, amazingly there doesn’t seem to be any sort of standard library already out even for calculus, although there is one for vectors and matricies I was able find.

The notation for working with vectors and matricies is really bad when you can’t use the + operator on arrays, and you cant do scalar multiplication. For example, here is a hideous expression for subtracting two vectors, A – B:

Math.vectorAddition(A,Math.scalarMultiplication(-1,B));

I have been looking for some kind of open-source project to contribute to for awhile, and even though my C++ is a bit rusty I would very much like to get into the code for V8 engine and extend the + operator to work on arrays, to get scalar multiplication to work, and possibly to get the ^ operator to work for exponentiation. These things would greatly enhance the utility of any mathematical javascript framework.

I really don’t know how to get involved in something like the V8 engine other than download the code and start working on it. Of course I’m afraid that since V8 is chrome specific, that without browser cross-compatibility a fundamental change of this type is likely to be rejected for V8. I was hoping someone could either tell me why this is a bad idea, or else give me some pointers about how to proceed at this point to get some kind of approval to add these features.

Thanks!

6

Your code isn’t too likely to find its way into V8. It’s a language change, and language changes happen only rarely, and if they do, it’s usually because a company, rather than a person or even a community, is pushing for them.

So … unless you want to train your VM muscles, I’d try and stay within the language.

You’ve hit on the two things that actually bum me out about JS sometimes. Wimpy numbers and no operator overloading. One of JS’s strengths however is ease of creating a less tedious interface than the perhaps more verbose/self-documenting interfaces from core functionality (or any icky interface you don’t want to deal with for that matter). Here’s a simple chaining addition interface I whipped up. I think I’d rather just add a subtract method and chain as necessary but when it sees a ‘-‘ it will subtract the next vector arg and its args are also overloaded to handle dropping an interface vector object directly in so stuff like this should work:

var vec1 = $v([1,2,3]);

vec1.val();//[1,2,3];
vec1.add([1,1,1],[1,1,1]).val();// [3,4,5]
vec1.add('-',[2,2,2]).val();// [1,2,3]

var vec2 = $([3,2,1]).add(vec1,[1,1,1],'-',[2,2,2]).val(); // [5,5,5]

And the func:

function $v(vector){ //produce basic chaining add/subtract wrapper

    return new VectorInterface(vector);

    function VectorInterface(vectorVal){

        if(!validateVector(vectorVal)){
            vectorVal = [0,0,0]; //no idea if you would want this as a default
        }
        //vectorVal is persistent

        //I use function hoisting to keep interface declaration clean
        this.val = val;
        this.add = add;
        //this.subtract = subtract; //so we can subtract without cheesy '-' args later

        //public methods

        function val(){
            return vectorVal;
        }

        function add(){
            var
                argLen = arguments.length,
                mod = 1
            ;

            for(var i=0; i < argLen; i++){

                var thisArg = arguments[i];

                if(thisArg.constructor.name === VectorInterface){
                    thisArg = thisArg.val();
                } 

                if(validateVector(thisArg)){
                    vectorVal[0]+= mod * thisArg[0];
                    vectorVal[1]+= mod * thisArg[1];
                    vectorVal[2]+= mod * thisArg[2];
                    mod = 1;
                }
                else if(thisArg='-'){ 
                    mod=-1;
                }
            }
            return this; //make it chainable for addition of other methods
        }


        //internal
        function validateVector(vec){
            if((vec !== undefined) && (vec.length !== undefined) && ( vec.length === 3 )){
                return true;
            }
            else { return false; }
        }
    };

}

It wouldn’t be that hard to add other operator methods or further overload the args to identify arrays of array vectors and chew through them as appropriate when added to the args.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật