6

I find the _.something(somevar, some_function_or_other_thing); "syntax" quite ugly. What are some good alternatives that use ruby-like iterators and similar stuff:

10..times(function(i) {
   console.log(i);
});

uppercasefoobar = ["foo", "bar"].each(function(i) {
    return i.toUpperCase();
});

Also, I am using node.js, so it should focus more on the code than DOM stuff.

phillips1012
  • 368
  • 4
  • 10
  • Probably nothing... There's a reason underscore does what it does. It's considered a very bad idea to pollute built-in types this way. You're essentially creating your own one-off version of the language that a) nobody understands and b) may break any given plugin/library. – user229044 Nov 06 '13 at 04:14
  • Also, your version of `each` is actually a `map`. Also-also, `10.times` is never going to work in JavaScript, that's a syntax error an no library can alter the constraints of the language's syntax. If you want Ruby-isms in your JavaScript, consider CoffeeScript. – user229044 Nov 06 '13 at 04:16
  • @meagar He's just providing an example – azz Nov 06 '13 at 04:17
  • 2
    If you're looking for Ruby-ness, I recommend you try out CoffeeScript. – Zhihao Nov 06 '13 at 04:18
  • Also, if you really really like Ruby, you should check out CoffeeScript: http://coffeescript.org/ I'm not a huge fan of Ruby in general, but I would strongly suggest that you get a good grip on javascript (if you're new to it) before using coffeescript so you understand the nature of the language. – Vinay Nov 06 '13 at 04:23
  • Ah damn. Apparently I should read comments. Just realized you're using CS. :-P – Vinay Nov 06 '13 at 04:23

3 Answers3

5

Suprised no one mentioned Lo-Dash. Lo-Dash is a superset of Underscore, adding numerous methods. John-David Dalton (creator of Lo-Dash) explains the key differences between the two libraries in answering an SO question here.

edit: found a detailed blog post "Say hello to Lo-Dash", detailing the background to the library and comparisons to other so called 'js utility belts'.

Community
  • 1
  • 1
Tony Cronin
  • 1,623
  • 1
  • 24
  • 30
  • 1
    Seconding this, nowadays Lo-Dash has superceded underscore and is superior in every way. It's a drop-in replacement too so you don't need to make any code changes, simply replace underscore with Lo-Dash and it works right out of the box! – Roy Feb 10 '14 at 10:25
4

Looks like you're looking for Array.prototype.map

uppercasefoobar = ["foo", "bar"].map(function(i) {
    return i.toUpperCase();
});
// uppercasefoobar == ["FOO", "BAR"]

With underscore.js you can also write:

_.range(10).forEach(function(i) {
    console.log(i);
});

Edit You can also use:

_(3).times(function(i) {
    console.log(i);
});

Not a pretty as ruby syntax, but it gets the job done.

In general, underscore provides an object oriented version of most functions, where:

_.something(variable, params);

Is equivalent to

_(variable).something(params);
azz
  • 5,852
  • 3
  • 30
  • 58
  • I was just providing an example of ruby-like syntax that could be implemented by said JS library (but I guess that one is already in ecmascript) – phillips1012 Nov 06 '13 at 04:08
  • @phillips1012 underscore also provided an OOP wrapper for most functions, see my edited answer. – azz Nov 06 '13 at 04:32
2

Found one myself! http://sugarjs.com/

(and yes, I do realize extending native classes essentially forks the language... that is my motive...)

phillips1012
  • 368
  • 4
  • 10