Hacker News new | ask | show | jobs
by noobermin 3730 days ago
There needs to be a javascript flavor/js based language that allows operator overloading...so that there can be something like numpy in javascript. I love js's semantics much more than python's so it'd be great to do quick and dirty numeric stuff in js.
3 comments

There is: https://kotlinlang.org/docs/reference/operator-overloading.h...

(Kotlin compiles to js or jvm byte code)

It is possible to overload operators in JS through a horrible hack of valueOf():

http://webcache.googleusercontent.com/search?q=cache:DKKZb1P...

Discussion: https://news.ycombinator.com/item?id=8677721

Aren't all numbers in javascript double precision.
There is a boxed 32-bit signed integer type in JavaScript, which can be instantiated by postfixing `| 0` to a number literal or variable of type number. You can also then convert numbers to an unsigned 32-bit integer by postfixing `>>>0`.

But yeah, for normal numeric operations, the default `number` double-precision is default.

That said, typed arrays of all the common types (u8,u16,u32 and their signed analogues) now exist in JS and are very important for performance reasons, particularly with WebGL and Emscripten.

You can also use typed arrays to get a single integer of the type you desire. For example, to convert a -1 to a signed integer, try this in your console:

    (new Uint32Array([-1]))[0]
You can then compare the result to what I mention above:

    -1>>>0
And this one gives what you would expect:

     var i = (new Uint8Array([-1]))[0]
     console.log(i)
You're right, but I guess you're being downvoted because nobody wants to hear it. JavaScript will never get anything like numpy, because it doesn't have integers. Doing mathematics without integers is like driving a car without wheels.
That is a pretty bold statement.

Combine typed arrays with BLAS and you have yourself a numpy (well, kind of). In Node you can move performance-critical code to C++, which does have integers. Browser support of typed arrays is a bit disappointing, but it's getting better.

Disclaimer: I'm developing https://github.com/mateogianolio/vectorious

(I didn't downvote you.) Yes, but it does not make operator overloading any less useful. In fact, I'd argue the opposite. In a language with only doubles you can use overloaded operators not just for matrices, complex numbers, rationals, etc., but to make it relatively painless to work with your own implementation of integers.