4 ms·
To each his own; I prefer JS/Ruby/Scala style map/filter/reduce functions for these sort of operations: let a = [1, 2, 3, 4, 5] > a.map(x => x * 10) (5)
by rajangdavis 8y ago
To each his own; I prefer JS/Ruby/Scala style map/filter/reduce functions for these sort of operations:
let a = [1, 2, 3, 4, 5]
> a.map(x => x * 10)
(5) [10, 20, 30, 40, 50]
> a.filter(x => x > 3)
(2) [4, 5]
> a1.map((x,i) => x + a2[i])
(5) [3, 6, 9, 12, 15]
I don't hate broadcasting, but it can get a bit cryptic at times. Once the filtering starts getting complex, it can be a bit difficult to read unless you start breaking things out into variables and chaining filters together.
For JS, something similar to pandas creating a new column based on operations of other columns would be to do map an array of objects (list of dicts) and use a (Schwartzian_transform)[https://en.wikipedia.org/wiki/Schwartzian_transform https://en.wikipedia.org/wiki/Schwartzian_transform] to create a new object property. I would love to look into how efficient these sort of operations can be, have had some success with this sort of thing for performing cosine similarity in the browser instead of creating sparse matrices with python.
- goatlover 8y agoI understand wanting to use map or filter when things get more complex. Pandas has filter and apply methods for that. I agree that the JS versions are quite nice with the arrow functions and method chaining. But for simpler operations, if you're doing a lot of vector computations, then map and filter bloat the code considerably when the mathematical notation is easy to read. Good luck with work on the JS implementation and efficiency comparisons. Pandas utilizes a Series object built on top of NumPy array for handling columns and rows. I don't know whether a similar approach would be helpful in JS.