82 points · by matthewmueller · 13y ago · 47 comments
It might be possible for a library like this to really catch on so much that a) other people use it, and b) even when they don't you get so much power you don't mind typing array(someOtherRealArray) all the time. JodaTime is like that.
But the thing that JodaTime has that Array.js doesn't is an enemy. JodaTime hates the built in Date, Calendar, etc classes in the JDK. It hates them and replaces them with extreme prejudice, and anyone forced to work with those APIs does not weep.
Array.js does not have an enemy, it has a friend it wants to help be a little better. It's a nice library, a helpful library. And that's why it will soon be forgotten, I'm afraid.
I think this is a pretty neat approach. It would be fun to play around with a version of Underscore that implemented a version of the idea. I make take a crack at it...
EDIT: apparently you are the creator of Underscore. Which makes me feel sort of silly for saying at least the second part. I think the first part stands, however.
_.chain(array).map(func).filter(func).reduce(func).value()
Taking a look at returning an array-like (or object-like, depending on what you pass in), for that same use case: _(array).map(func).filter(func).reduce(func)
... could be nice. The devil's in the details of interoperability with other libraries, as other folks note in this thread.Not that it couldn't happen, of course, I don't know if he still is in charge or not. But it'd be amusing.
The other aspect to this of course is that using es6 proxies, the library could have been made to behave exactly like the native array even without using one.
user.max("age"); looks sexy but I'm sure type of error you get from user.max(func)l is much better and more clear
It would be be nice if Underscore had a bit more sugar like that. But, I also know how problematic it can be to properly parse or evaluate a string into code.
--edit--
it is using the to-function library to do this: https://github.com/component/to-function
select((o) -> o.x > 30)
Seriously, it's great. :)You could just as easily approach this lib the same way: array(['apple', 'orange', 'pear']).map(fn);
The only difference with this library and other arrays/client-side collections I've come across is that there's less indirection, which I find less confusing, ie. am I working with a collection object or am I working with an array?
Of course there are caveats (things to consider) when using this lib which I outlined in the docs.
[1] http://knockoutjs.com/documentation/observableArrays.html
var arr = array([1, 2, 3, 4]); // An Array.js "array"
var arr2 = []; // A real JavaScript array
It's definitely too heavy for most array use cases. Sometimes all you need is a simple list.On the other hand, interop with D3.js has been ideal as it generally assumes that any non function parameter is an array, so there I"ve had more success and would expect array.js to similarly work well.
While to-function is cool and your code is shorter and more readable. You must be aware that you miss
- static code analysis such as synax checking - compile time optimizations (because the AST is missing) - static code analysis tools - code coverage checking tools
I would recommend using real functions, or using clojurescript or coffeescript in order to get shorter code.
I mean, if there is one thing I do like about Java (when I can't use Scala for some reason), is those utility belt libraries (and JodaTime of course). Whenever I hit a wall trying to do something in plain old Java, Guava / Apache Commons seems to read my mind and have a data structure, or a utility to the rescue.
Do you think it's a good thing to strive for? Do you see it happening?
i don't know how this relates to Apache commons, JodaTime, or Guava however, but i just wanted to add a little context.
Pretty much the same functionality, done three years ago.
If you don't need the events then lo_dash/underscore is probably the better tool.
lines like this:
DB()
.insert(what)
.find( DB(".contentDetails.regionRestriction.blocked.indexOf('US') > -1") )
.select('id')
.each(Timeline.remove);There's not many things I miss after switching to CoffeeScript from Python, but this is one of them.
This is a place where CoffeeScript or LiveScript really shine. In LiveScript it would look like:
users.select (.age > 20)
The main difference seems to be that Backbone expects objects as members of the collection.