-
Notifications
You must be signed in to change notification settings - Fork 2k
Shortcuts for applying and for currying #251
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
Comments
Splats will usually get you what you want: |
What you suggested compiles to
Where I'm looking for
Anyway, I just forked the repos and made the necessary changes along with test and everything works. If you guys wanna take a look ... So I added the following unary operators
Compiling to
|
yeah, I re-edited it after I realized the mistake. |
So, I removed the
Cleanly compiles to
Which is shamelessly ripped from underscore |
+1 for currying support. not so sure whether the apply is necessary ? |
Just made it a bit more useful. Now the following lines are equivalent :
Not much, but it makes things a bit cleaner. |
Alright, I've splitted the two extensions into two completely seperate branches (curry and apply), each applicable to latest trunk from Jeremy. The main difference is that the currying operator I thought the Now, I think the |
I was recently trying to implement a construct closer to normal function definition, to make it a bit more intuitive. Something that would set the new function binding clearly apart from the bound arguments. Something like :
So rebinding to
Or even better,
but can't seem to get it to work. I know, |
I think it would be great if you implement these additions to Coffee as extensions! I don't think we need a new language construct in the core. It may just be me, but if you can do it in one-or-two lines of JavaScript (think |
Many core language features can be implemented in a few lines a code, and currying/applying is really a core features in some functional-oriented languages. Also, I tried using these constructs in real-world hacking and it does an incredible job at improving the readability in applicable situations. I will take a look at the extensions, though. It sure should be nice if I can implement them as such. |
isn't there a potential inconsistency with your different currying formats ? F: foo <- (@, "Hello", "World!") F: foo <- (@, ["Hello", "world"]) x: [a,b] F: foo <- (@, x) # what happens in this case? Might be best to only have one format to avoid confusion. |
Well, no. At compile time, the parser doesn't know if the arguments are arrays or not. For the moment, it is compiled as such :
That's also the reason why I was recently working on another form, the last one I invoked, which is also more consistent. |
ah yes you're right. |
In the new form, everything in the parentheses are arguments (with the familiar splats for arbitrary number of arguments) and the function's new bound object is right before it, apart with a colon (could really be anything). This makes the construct closer to function definition (no arguments in arrays) and allows simple forms for the simpler cases :
Where, in the first form, only function rebinding occurs. In the second, only arguments currying. |
and how might you do both ? |
Like that :
I'm trying to implement those as an extension, mentionned in #243. Seems promising. |
sorry, but I don't like that very much as the : looks like an assignment. |
You know that's just arbitrary, it could really be a comma or anything. Nothing's settle for now. |
ah yes a comma would work ok. regarding the argument currying, will you support currying of the arguments to a function from first to last ? I.e. f: (x,y,z) -> x*y*z g: f <- (1,2) ok g(a) == f(1,2,a) # true |
absolutely, that's exactly the way it'd works! |
matehat: You mentioned that you've found this new syntax to be useful in increasing readability in real-world situations. Mind sharing some examples of how you're currently using it? |
For instance,
Again, stuff in parentheses are arguments and the object before is the bound context and both can be used alone. This syntax isn't pushed to my repos yet, still has some stuff to tweak. |
syntax suggestions Array::slice <- @, (0) Array::slice & @, (0) Array::slice << @, (0) Array::slice ~ @, (0) Array::slice <-> @, (0) I quite like the |
Ah, finally. Here, in this branch: http://github.com/matehat/coffee-script/tree/curry I made the proposed syntax work properly. BTW, extensions could not be used here since it involved plugin a little deeper into the lexing process. weepy: I really don't mind. Any of those can do and could easily be swapped in place with what I've done. |
Hey matehat -- this is probably fodder for a different ticket, but looking at your branch, your patch is a nice clean language addition that touches the lexer, the grammar, and the syntax tree. Do you have any thoughts about the way the compiler is structured for this? Was it easy to extend? Did you run into any counterintuitive bits or get stuck on anything while working on your patch? |
Well, it wasn't counterintuitive once I convinced myself to go through and understand all the files implied in the compiler. My first patch, a few days ago, was only messing with the grammar and some custom nodes, because I had a limited understanding of the compiler. I later found myself not being to do most of things I tried, so I read. This is really good stuff, things are pretty straightforward once you read through the whole code. Some comments though:
Oh, and that might have helped : I have, in the past, written a compiler for a custom template language and most of the steps here were present then. |
For the record, my recent addition for arbitrary number of splats in a function call was applied in my curry branch, so one can now do :
|
so how do you determine which splat group an argument will fall ? durh: ignore me .... |
The bind operator is now on master. You can see the test case in Closing the ticket. |
Hi,
Since some hardcore scripting often requires the use of
Wouldn't it be nice if we could have it as:
The punctiation is really a suggestion for the moment. Ideas, anyone?
The text was updated successfully, but these errors were encountered: