Underscore.js: Functional Programming for jQuery
documentcloud.github.com
documentcloud.github.com
One of the neatest things about jQuery is the way all of the jQuery functionality lives in the "jQuery" namespace, with $ just set up as a convenient shortcut. If you don't want the $ in your global namespace (but still want it in your own code) you can do this:
jQuery.noConflict();
jQuery(function($) {
// Code that uses the $ shortcut
}
Feature request for underscore: something similar. If I'm already using the _ for something I'd like to still be able to use the underscore library, but bound to a different symbol. Underscore.noConflict();
Underscore(function(_) {
// My code that uses _
}The Underscore page now has the download for version 0.1.1, with a noConflict function that returns the "_" variable to its previous value.
var myUnderscore = _.noConflict();Blog Announcement: http://www.documentcloud.org/blog/2009/10/28/underscore-dot-...
Git Repo: http://github.com/documentcloud/underscore
Test and Benchmark Suite: http://documentcloud.github.com/underscore/test/test.html
<h1>_('WELCOME_USER', user.firstName)</h1>
<p>_('WELCOME_INTRO_TEXT')</p>
etc...Check out http://github.com/ShiftSpace/promises for bringing functional sanity to AJAX requests as well support for lazy values:
function example() {
var remotes = ['a', 'b', 'c', 'd', 'e', 'f'];
var result = "";
while(remotev = remotes.shift()) {
result = add(result, get(remotev)); // get is an async request! ;)
}
show(result); // -> console.logs "abcdef"
}
No callbacks, your code looks like (and _is_) regular javascript even though this triggers 6 requests. AJAX callback code is the new spaghetti and this is an antidote.And finally a http://github.com/ShiftSpace/set for a Set datastructure :)
You might also be interested in http://osteele.com/sources/javascript/concurrent/ for another approach to this. [later, after reading more:] Actually, Promises looks much more complete and useful.
edit: by that I mean it's useful without jQuery. It's already been added to Narwhal server-side JS package manager: http://github.com/kriskowal/underscore
* "reduce" instead of "inject" (inject is now an alias).
* Other aliases added for Javascript 1.6+ standardized names (forEach, filter, every, some).
* "compose", and "lastIndexOf" added.
It's not tied to jQuery and approaches things in a particular way.
Oliver Steele's Functional is a great library too, and bits of Underscore were inspired by it. The main difference between the parts they have in common is that Functional.js extends the core Function prototype, while Underscore avoids altering any core objects.
I eventually found that I was accumulating so many string functions that, in a version of this for OpenLaszlo here http://github.com/osteele/lzosutils, I broke them into a separate file.
One thing that it would be nice to agree on is the standard names for some of these things under Javascript. What should reduce be called? Inject? Foldl? I've followed the Prototype/Ruby naming conventions, but it might make sense to provide all of the commonly-used aliases -- especially when you're not extending core objects, and don't need to worry about cluttering them up. What do you think? Does the same function under 5 different names run as sweet?
I spend most of my day as a Ruby developer, but when I'm doing frontend work, the thing I miss most in Javascript is Ruby's incredibly simple, powerful list operations. While most of these have existed in some form for awhile now, I've never seen such a comprehensive implementation in just 5.2kb (minified) of very clean, simple, well-commented code.
This is probably going in every project I work on from here on out. Thanks for putting all of this together.
>> _.first(array)
>> "Convenience to return the first element of an array (identical to array[0])."
How is _.first(array) more convenient than array[0] ? :/ array[array.length - 1];>> _.map(listOfLists, _.first)
vs.
>> _.map(listOfLists, function(array) { return array[0]; })
for example.
cat jquery.js functional.js > myfatquery.js and be happy.
Part of the idea is to make it easy to add functional utility methods to an existing project without making any other assumptions. You can use it with whatever OO scheme you've got cooked up, any Ajax library, and any DOM-manipulation framework.
I have found Prototype much better for a "web-app" framework - I can build up complex controls and effects with the "OOP"/Inheritance framework, and I have access to all the functional tools I'm used to from Ruby.
JQuery, on the other hand, has always felt "lighter" - really great for a quick Carousel control on a marketing site, or a pop-up effect or something like that, but it quickly turns into spaghetti with anything more complex.
I'd be interested in other people's views on this split, especially people with experience using a whole toolset like yourself.