News for hackers. Stuff that matters.
Re:Array.js: A better array for the browser and Node.js by jashkenas on Friday February 15, 2013 @04:53PM (#5228902)
(disclosure, I work on Underscore)

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

Re:Array.js: A better array for the browser and Node.js by javajosh on Friday February 15, 2013 @05:09PM (#5228977)
Creating a new datatype would pretty seriously go against Underscore's implicit philosophy of being a set of functions you bring to bear on ordinary data structures. In other words, you can try, but I suspect your pull request be voted down fairly decisively.

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.

Re:Array.js: A better array for the browser and Node.js by jashkenas on Friday February 15, 2013 @05:15PM (#5229007)
Probably, but I'm thinking that it might be an approach worth taking a look at as an alternative to the current:

    _.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.
Re:Array.js: A better array for the browser and Node.js by javajosh on Friday February 15, 2013 @08:11PM (#5229846)
So in order to save one call to chain() you'd use a non-standard object over a native array? What would underscore methods return, then? Perhaps it could do type detection and return what it gets, but then you have the issue that the executing code needs to be aware of that too - which means a type check at the caller before invoking the underscore expression. Needless to say, I think that's a lot of added complexity for not a lot of benefit.

Heck, if you really wanted to adapt something from Array.js you could do the naive (but I think correct) thing which is to add a flag to all underscore methods indicating whether or not the method should return an "Underscored" object or the basic data type. This could even be an initialization parameter for Underscore.

What does this buy you? I can only think of two things. First is they get a more object-oriented feel to their code.

    var foo = _.filter([1,2,3], func, true);
    // later on...
    foo.filter(func2);
I guess the first question I have is, what does line 3 do? Are we mutating foo? Or returning something new? In which case we've managed to save precisely one character

     _.filter(foo, func2);
Even worse, we're saving the wrong character. One undersung quality of having one global object per major library is that, in the code, you get a big contextual clue about what's going on from the way it's invoked. Contrast these two lines:

    foo.filter(func2);
    _.filter(foo, func2);
Try to imagine that you're encountering these lines deep in the bowels of the code. Which one is easier to understand?

The second thing you get is a (what I think is) esoteric benefit of being able to "ship" underscore functionality with the data to far away places. This is only an issue in full require()-controlled programs where there might be some communication between closures, and one side wants to use underscore in that context, but not add a dependency. Personally, I think being able to do that is just asking for trouble, as, again, your calling code maintainers are going to be totally befuddled by running into objects doing things that no other objects in that side are doing - or worse, doing something that looks familiar, but isn't.

Re:Array.js: A better array for the browser and Node.js by knowtheory on Friday February 15, 2013 @08:29PM (#5229892)
You're saving at least two characters there ;)

I think sensible semantic naming still helps in this circumstance.

    yummyBars = candyBars.filter(function(bar){ return bar.yummy; });
makes it clearer that the thing you're getting back is like the thing you had previously.

One place this could be useful is in Backbone.Collection where the array-returning mixins from underscore all hand back bare arrays, on which devs must then switch to _ prefixed functions (although you're right that this does keep a clear conceptual separation between collections and other arrays).

Re:Array.js: A better array for the browser and Node.js by javajosh on Friday February 15, 2013 @08:51PM (#5229952)
I get a warm and fuzzy when I know what that method does, that it's not some random filter that could mean something totally different. (And yes, this does mean that my code is not very object oriented at all!)
Re:Array.js: A better array for the browser and Node.js by knowtheory on Saturday February 16, 2013 @11:17AM (#5231895)
That's okay! Use the right tool in the right place. And agreed, I like knowing where a function comes from.

I'm not totally convinced by the move to 100% functionalist constructs (there are some places where OO is nice), but it's a nice stylistic practice.

Re:Array.js: A better array for the browser and Node.js by nadaviv on Tuesday February 19, 2013 @07:41PM (#5248206)
It has one important downside tho - because you can't chain calls, your code ends up much less readable: It needs to be read from the inside-out, and the functions you're applying on the array are in reverse order compared to the order of execution. Its also harder to to see which arguments go to which function.

Compare

    _(array).map(func).filter(func).reduce(func)
and:

   _.reduce(_.filter(_.map(array, func), func), func)
Re:Array.js: A better array for the browser and Node.js by dribnet on Friday February 15, 2013 @05:28PM (#5229080)
> The devil's in the details of interoperability with other libraries, as other folks note in this thread.

Indeed. I should have also mentioned that editing angular.js to work is fairly straightforward (~5-10 LOC), and this is probably fairly typical. So if you are willing to patch this isn't necessarily a blocker, and done right could probably be submitted as a reasonable merge request.

Re:Array.js: A better array for the browser and Node.js by steveklabnik on Friday February 15, 2013 @05:26PM (#5229066)
This is where I point out that the idea of jashkenas getting a pull request rejected from Underscore would be pretty funny.

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.

Re:Array.js: A better array for the browser and Node.js by knowtheory on Friday February 15, 2013 @08:03PM (#5229820)
Yeah, as far as i'm concerned, jashkenas is still BDFL for DocumentCloud's major libraries.

I've taken over maintenance of the main DocumentCloud platform and document viewer repos, as well as docsplit.

And although I've been shirking on jammit, but I intend to cut a new version of the gem soon.

Re:Array.js: A better array for the browser and Node.js by necubi on Friday February 15, 2013 @05:19PM (#5229032)
jashkenas is being humble. He's actually the creator of underscore (and backbone, and coffeescript, and docco...).
Re:Array.js: A better array for the browser and Node.js by kybernetikos on Friday February 15, 2013 @07:00PM (#5229569)
The way I did it was to write lots of functions that take e.g. an array (the same approach works for other things) as their first argument, then a function that I called unbind which converts a function of x arguments into a function of (x-1) arguments where the first argument is replaced with the this pointer. Then you can have a function ('bless') that copies all of those 'unbound' functions onto a real data object or prototype. That way you get the best of both worlds and it's up to the user as to what they bless.

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.

Re:Array.js: A better array for the browser and Node.js by matthewmueller on Friday February 15, 2013 @08:14PM (#5229853)
I would love to see underscore integrate this style of API.
Re:Array.js: A better array for the browser and Node.js by msoad on Friday February 15, 2013 @10:44PM (#5230284)
This involves a lot "String Programming" which makes code hard to test and debug.

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