Hacker News new | past | comments | ask | show | jobs | submit login

OT - I can't help but mentally shorten code snippets with underscore/whatever. For example, I see this -

for(var index = 0, length = listItems.length; index < length; index++) { listItems[index].remove(); }

and I think to myself -

_(listItems).invoke('remove')




That's awesome! Is that from underscore.js?


Yup. I love the philosophy underlying underscore. The functions are ridiculously simple, but so powerful to use once you grok it.


>The functions are ridiculously simple, but so powerful to use once you grok it.

As a Lisp hacker and current learner of Haskell, this makes my spleen rupture.

It's just functional programming 101, seriously.

That's all it is.


Just to let you know, while you're 100% right, I downvoted you for unnecessary snark.


Ack, didn't mean to offend you either. I like Lisp too (I'm learning Racket). I was just trying to explain how underscore.js makes me feel.


Maybe your approach is better for the advocacy of functional programming anyway, makes it less intimidating.


I'm sorry, but can we please stop with the 'grok' thing?


I apologize, I didn't realize it's offensive to you/others. Seemed like a perfectly cromulent word.


It might just be me. I just find it obnoxious.


Do you know the reference?


It's from Robert A. Heinlein scifi book "Stranger in a Strange Land".

In the novel native martians do exist and they are very smart. "Grok" it's the martian word for "water" and "to drink". Water is a scarce resource there, and, like here, very important for life.

Drinking is putting water inside you but also occurs that this water becomes part of you.

Then, in that society, drinking is used as the metaphor for understand something in a level that becomes part of you.

I'm a native spanish speaker and I don't know if that word sounds bad in english, but for me the metaphor is so powerful and beautiful that I like it very much.


I have been so conditioned by posting on HN to not reply with the banal, 'this', or, 'good one', that it almost seems wrong to just say, 'thank you for that post'.


Thanks ;)


FYI I knew the reference, but was wondering if the agitated OP did. You explained it better than I would have.

"To really understand something REALLY well".


Perhaps it's just been by a series of improbable coincidences that in the years I have read forums, IRC logs, books, and articles, I've only seen the term here on HN.


Could be a case of Baader-Meinhof [1], or maybe you haven't been socializing in right forums. The word "grok" has been in popular use on Usenet since the early 1990s at least, probably before, since it's from a 1970s Heinlein novel.

[1] http://wikibin.org/articles/baader-meinhof-phenomenon.html


"grok" thing? It's in the OED


Or, in LiveScript:

    map (.remove!),list-items


How does that work?


http://underscorejs.org/#invoke

In simple terms, it goes through the array, and calls the named function on every element. You could read it as - "For this array, invoke the 'remove' function on every element."


that's funny, I'm the other way around.. whenever I see syntactic sugar implemented via javascript, I unroll it in my head and consider it overhead, compared to sending a bunch of more bytes down the wire which hardly matter after gzip.


Underscore uses native methods for map/reduce/forEach when possible. For a modern browser the performance difference is comparable, or even faster for a map than an unrolled for loop:

http://jsperf.com/native-for-loop-vs-array-foreach-and-array...


TIL javascript functions are syntactic sugar.


And TIL stands for what, exactly? Sure you would not simply equate "syntactic sugar" with "syntactic sugar implemented in javascript".. so what are "TIL javascrip functions"? I tried searching, but no luck, woth none of the permutations. There are too many acronyms for TIL to just blindly guess.

My point basically was that

_(listItems).invoke('remove')

is actually short for

* [content of http://underscorejs.org/underscore-min.js] _(listItems).invoke('remove')*

and even if you removed all non-called parts from underscore, that's still a lot more complex than

for(var index = 0, length = listItems.length; index < length; index++) { listItems[index].remove(); }

it's just hidden, out of sight and out of mind.


TIL stands for Today I Learned.

They were attempting to use a meme acronym sarcastically -- an example of content-free commenting that works well for karma elsewhere but hopefully doesn't gain a foothold here.

On topic, I agree with your amplification of your point.


If you use memes on HN, you're going to have a bad time.

(They tend to be downvoted to oblivion, 'round these parts.)


Oh. Thanks, I get it now.

I guess while syntactic sugar is usually considered a functionality of the language itself, it surely is used in the way I meant it, too... random search result:

http://www.developerdrive.com/2012/05/an-introduction-to-und...

"In this part of the tutorial we’re going to take a look at some of the syntactic sugar that Underscore gives us to work with array-like collection."

And of course, even if I had been utterly wrong for using the term, my point would stand unmoved, but you know that :)


I can get where you're going with this, but as someone who uses underscore everyday I have to say in this particular case I don't agree with you.

Underscore as a production library is 4kb. The functions are terse, and its extremely easy to understand what they're doing - nothing is generally hidden (this example is pretty easy to understand, yet one of the most "complex" uses of underscore I've seen).

At the end of the day on a production javascript app of multiple thousand lines of JS, it likely actually saves me bytes over the wire from not having to write "for(var x = 0...." every time I want to iterate through something or filter an array or find if an array includes something etc (There's around 70 bytes removed in this single example).


Well, of course it matters how often you do it. I would not do it in loops, or often called event handlers, such as scrolling. If you do it once on page load, sure, it hardly matters.

But if you do it a lot in your code, that just means you can easily copy and paste it from a nearby place? At least that's what I often end up doing.


I've been using underscore for a few months and had no idea that you could call _ as a function until today. Pretty amazed that I missed this all along. Thanks for this!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: