Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chaining functions using operator #1251

Closed
werg opened this issue Mar 30, 2011 · 13 comments
Closed

Chaining functions using operator #1251

werg opened this issue Mar 30, 2011 · 13 comments

Comments

@werg
Copy link

werg commented Mar 30, 2011

Here's another idea for how to make function call chains more beautiful:

$("#something") >> height 144 >> children() >> addClass "something-else"
      >> animate {width: 40}

would compile to:

$("#something").height(144).children().addClass.("something-else").animate({width: 40})

Now that was my initial idea, obviously it would conflict with the bitwise shift operator. I personally would gladly sacrifice bitwise shift (or give it a different operator), but not sure how popular that would be. Alternatively can somebody think of another operator that could unambiguously be used for this task?

Ideally one could continue in a new line as in the example above, but as far as I know this only works if the operator is in the preceding line?

@dvv
Copy link

dvv commented Mar 30, 2011

$("#something") | height 144 | children() | addClass "something-else"
  | animate {width: 40}

resembles piping and doesn't clash bitwise shift

@michaelficarra
Copy link
Collaborator

@dvv: though, it clashes with the even more common bitwise OR

@michaelficarra
Copy link
Collaborator

@werg: I don't like it.

$("#something")
  .height(144)
  .children()
  .addClass("something-else")
  .animate
    width: 40

What's wrong with this?

@dvv
Copy link

dvv commented Mar 30, 2011

@michaelficarra: ... it clashes with the even more common bitwise OR

which could be consistently (&& -> and, || -> or) named bor and freed

@michaelficarra
Copy link
Collaborator

@dvv: Gross. Also, CS doesn't drop && and || support, it just adds the and and or aliases.

@dvv
Copy link

dvv commented Mar 30, 2011

Any reason for that?

@michaelficarra
Copy link
Collaborator

@dvv: Yeah, some people like using those operators. They're familiar from (pretty much all) other languages. And it helps smooth out the CS learning curve. We don't force people to use Class::method or @property unless they want to. They'll learn to use them, though.

@dvv
Copy link

dvv commented Mar 30, 2011

Very well.

Any new thoughts on sugaring callback-style chaining? Whether it's considered feasible to have such in CS at all?

@jashkenas
Copy link
Owner

Yes -- this is a duplicate of an old ticket / branch of CoffeeScript ... never merged to mainline because piped operators are strictly speaking less powerful than both regular function calls, and method chaining style -- being only able to pass a single argument.

#281

@dvv
Copy link

dvv commented Mar 31, 2011

I must have failed to pose the question, or can't parse the answer -- I don't mean sugaring chaining style for vanilla sync case which is pretty sweet already.
I mean sugaring node.js style -- f(a, b, c, cb){...; cb(err, result){...; next()}}, which is pretty hard read/maintain w/o either helper library, or syntactic sugar. I saw some projects that compile JS to JS to help living in async world, but compiling JS to JS just for that seems rather quirky to me. OTOH, CS is already a compiler one have to run to execute his coffee code. That's why I find it feasible to consider ways of sugaring node.js flow control style in CS, which could be a killer feature.

What do you think?

@werg
Copy link
Author

werg commented Mar 31, 2011

How hard would it be to ad another operator like --> or ==> or maybe even something like --|?

I just had the idea that including a directional operator would make long chains with an assigned result value more clear in some situations, as in:

r = width() <== filter "bar" <== children() <== css {...} <== $("foo") 

@jashkenas
Copy link
Owner

@dvv: Try reading the closed issues before proposing it as a new idea.

#241, #287, #350, #656, #289.

@dvv
Copy link

dvv commented Mar 31, 2011

Never meant to pretend it's brand new idea -- just asked what's new in this field. Thanks for retrospective analysis. I guess nothing new since then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants