News for hackers. Stuff that matters.
Re:CoffeeScript One Liners by Argorak on Thursday June 02, 2011 @06:19AM (#2611356)
Nice collection of examples.

But: I am still struggling with that talk about 'expressiveness' that seems to take hold of every programming piece. To take his first example: To anyone who has an advanced programming vocabulary, his notion of "expressiveness" thing can be reversed. If I read code like this:

    i * 2 for i in [1..10]
I immediately think: "Well, this is a `map`, why doesn't he spell it out?". So my personal "expressive" version is the following:

    [1..10].map (i) -> i*2
In my opinion, it is also clearer, because it can be read from left to right.

I do understand that this might not be the case for other people, but that doesn't make the one option 'more expressive' then the other.

Actually, in the long examples, I find the suffix version extremely odd:

    console.log "Happy Birthday #{if i is 3 then "dear Robert" else "to You"}" for i in [1..4]
I have to read the whole line before I actually find out that this is an iteration. (Besides the fact that putting a conditional into a string interpolation just to make it a oneliner seems a bit odd)
Re:CoffeeScript One Liners by thenduks on Thursday June 02, 2011 @07:01AM (#2611433)
I completely agree. English is English, code is code. I prefer `map`, especially when the function is any more complex than `i * 2`.

And suffix conditionals (and the like) are mostly useful to me when it's much shorter and sweeter, but doesn't lose any clarity:

    return false if somethingBad
Re:CoffeeScript One Liners by endtime on Thursday June 02, 2011 @08:52AM (#2611733)
>English is English, code is code.

I'm not sure this kind of 'render unto Caesar' thinking is conducive to literate code.

Definitely agree with you on the suffix conditionals.

Re:CoffeeScript One Liners by thenduks on Thursday June 02, 2011 @12:20PM (#2612607)
I just don't think that forcing code to read like English necessarily makes the code any more readable. Sure, if you read it out loud it comes out sounding somewhat like a sentence. That's cool and everything, but when I'm reading code to, you know, understand it... I want it to be succinct and completely unambiguous.

Here's a great example from Ruby: `unless is_allowed == false`

Reads like English? Check. Sane code? No way. As @heycarsten put it: http://twitter.com/#!/heycarsten/status/28042439329

So I'm suggesting that 'reads like English' isn't the goal you should be aiming for. It's sane, readable code. Sometimes that's English-like (`return x if y`) and other times it's code-like (`list.map( func )`).

Obviously there's room for personal preferences to color where you fall on this (and most other things) :)

Re:CoffeeScript One Liners by endtime on Thursday June 02, 2011 @01:05PM (#2612822)
That's not really a non-irrelevant example - it isn't different from a double-negative, which isn't a good way to write non-confusing English.

I don't see a problem with something like `unless forbidden`, though.

Re:CoffeeScript One Liners by thenduks on Thursday June 02, 2011 @03:12PM (#2613445)
Fair enough, I'm only saying that 'like English' isn't some kind of holy grail of understandable code. I like code, I can read code, not every line needs to be turned into a sentence.
Re:CoffeeScript One Liners by edanm on Thursday June 02, 2011 @08:10AM (#2611609)
"I do understand that this might not be the case for other people, but that doesn't make the one option 'more expressive' then the other."

Look, I like the comprehensions better. I also think if you polled a large group of programmers and asked them what the code does, more of them will understand the comprehension than the map. Does that mean it's more expressive? I don't know, know one here agrees on a definition for expressiveness. But that's at least one positive (IMO) of comprehensions.

Also, stacking multiple comprehensions together is much easier (cartesian multiple style). E.g.: (i,j) for i in list_1 for j in list_2.

This is much easier to write and understand with comprehensions.

* Note - I know comprehensions from Python, let me know if the cartesian multiple example doesn't work in CoffeeScript.

Re:CoffeeScript One Liners by jessedhillon on Thursday June 02, 2011 @02:51PM (#2613345)
> more of them will understand the comprehension than the map

What? Not if you poll programmers who had to go through SICP or take a real CS curriculum.

Re:CoffeeScript One Liners by catshirt on Thursday June 02, 2011 @10:33AM (#2612095)
well, i prefer left to right and "map". clearly it's subjective.
Re:CoffeeScript One Liners by ricardo_ on Thursday June 02, 2011 @06:24AM (#2611366)
IMO expressiveness has to do with being close to natural language: "multiply number by 2 for every number in 1 to 10". "map" is CS vocabulary.

The conditional could be placed outside the string and still be a one-liner. It's there mostly for "flow".

Re:CoffeeScript One Liners by Argorak on Thursday June 02, 2011 @06:46AM (#2611404)
We are doing applied CS here, don't we? So lets express us in that language. Also, why do we accept a*2 then? This is short mathematical notation, also a specialized language.

Every field evolves domain languages for a reason: it adds the benefit of naming actions instead of implicitly doing them.

"Being close to natural language" is a red herring. You can try, you will never get there and you will loose any benefit of a domain language.

Also, my point was that interpretations of expressiveness differ vastly, so we should stop claiming that something is more expressive then the other.

Re:CoffeeScript One Liners by rimantas on Thursday June 02, 2011 @06:59AM (#2611428)

  > We are doing applied CS here, don't we?
Yes. And CoffeScript prefers comprehensions over the maps. From the docs: > Comprehensions should be able to handle most places > where you otherwise would use a loop, each/forEach, > map, or select/filter

In this case I prefer that way also because it puts the main thing first.

Re:CoffeeScript One Liners by adamtulinius on Thursday June 02, 2011 @06:43AM (#2611398)
http://en.wikipedia.org/wiki/Expressive_power

"In computer science, the expressive power of a language describes the ideas expressible in that language."

So, language expressiveness is about what can be expressed in a language, and not really about whether you type "multiply number by 2 for every number in 1 to 10" or use map, since that expresses the same thing.

Re:CoffeeScript One Liners by Argorak on Thursday June 02, 2011 @06:48AM (#2611409)
It actually doesn't express the same thing. Map has no necessary notion of ordering, although most of the time, it behaves like the for-loop.
Re:CoffeeScript One Liners by ricardobeat on Thursday June 02, 2011 @06:47AM (#2611406)
"In computer science". I'm referring to expressiveness from a linguistics point of view - understanding an expression without depending on complex previously learned concepts.

Also, I'm not a native English speaker so I might be using the wrong words to express myself.

Re:CoffeeScript One Liners by Argorak on Thursday June 02, 2011 @06:54AM (#2611421)
I fail see how map is a difficult concept, despite being a word that needs to be learned. I do a lot of training for Ruby and usually introduce enumerable.rb first to all newbies. Usual reaction: "cool, I finally have a word for that". It gives programmers the ability to _express_ what action they are doing there, instead of leaving them free-floating in a for-loop-soup.
Re:CoffeeScript One Liners by ricardobeat on Thursday June 02, 2011 @07:04AM (#2611445)
For example, ask your parents about map, then the comprehension, which one will they be able to understand?

It's about usability: the most ingenious programmer will be more efficient using a simpler, straight-forward interface as long as it accomplishes the task. You're a user of a language's interface/syntax.

But we are drifting off here. I'll avoid using "expressive" the next time around :)

Re:CoffeeScript One Liners by jessedhillon on Thursday June 02, 2011 @02:57PM (#2613377)
> For example, ask your parents about map,

Since when is that the standard for a programming language? Some tools are made for expert usage -- they require training and foreknowledge. Programming languages weren't made for your mom.

Re:CoffeeScript One Liners by thenduks on Thursday June 02, 2011 @03:18PM (#2613474)
The funny part is that your parents probably wont understand the comprehension either. Even if they were likely to, I prefer to leave things the way they are when the only reason to change it is "now my kid brother can do it!"
Re:CoffeeScript One Liners by ricardobeat on Thursday June 02, 2011 @03:33PM (#2613560)
Am I on reddit? y u no read previous comments?
Re:CoffeeScript One Liners by Argorak on Thursday June 02, 2011 @07:17AM (#2611475)
Actually, both of my parents are programmers of the old school, so they even understand the difference between foldr and foldl.

I am actually in favor of having both options, so we might be on the same page actually. What I am opposed to, is the notion of adding a positive label to the less specialized version and a negative to the other.

Re:CoffeeScript One Liners by BasDirks on Thursday June 02, 2011 @07:35AM (#2611506)
map is an easy and powerful concept, but a comprehension resembles natural language better. I agree though that map is more _expressive_ (a more concise and comprehensive way of communicating).
Re:CoffeeScript One Liners by Isofarro on Thursday June 02, 2011 @06:16AM (#2611351)
A function walks into a function, turns to one of the members and asks "can I stash my privates in here?"
Re:CoffeeScript One Liners by acangiano on Thursday June 02, 2011 @11:56AM (#2612495)
Re:CoffeeScript One Liners by btipling on Thursday June 02, 2011 @09:18AM (#2611836)
>" this is only acceptable for application start-up routines"

You gonna come spank me if I do it synchronously and it works for me? I hate when people are so forceful with their opinions.

Re:CoffeeScript One Liners by ricardobeat on Thursday June 02, 2011 @09:49AM (#2611934)
It's not just an opinion, blocking your event loop will halt the world in nodejs.

Now give me your address, please.

Re:CoffeeScript One Liners by thenduks on Thursday June 02, 2011 @03:26PM (#2613523)
Yea, he means acceptable from a performance standpoint since in node the event loop will just stop and wait for that to finish before doing anything else. http://nodejs.org/#about

The `*Sync` methods are really only there for convenience sake, so you don't end up with 'banana code' around your whole app because of your initialization (reading config files, etc).