LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Consider adding an apply operator for manually curried functions

Open danielo515 opened this issue 6 years ago • 10 comments

Hello,

I know that you have support for curry, but that involves extra wrapping around every function call. I use extensively manually curried (unary) functions. I would love to have an operator to express that I'm calling a curried unary function. Something like this

concat $ 1 2 3
// Which will compile to
concat (1) (2) (3)

I know this is not very mainstream, and that most people prefers the other way. Maybe if a macro system is implemented I could just create my own.

Regards

danielo515 avatar Jun 15 '18 12:06 danielo515

This is pretty easy to do as a function, isn't it?

uncurry = (f, ...args) -> args.reduce (<|), f

myfun = (a) -> (b) -> (c) -> a + b + c

uncurry myfun, 1 2 3 # => 6

rhendric avatar Jun 15 '18 13:06 rhendric

Yes, but it has the same runtime overhead as just using the curried function. I want it on compile time

danielo515 avatar Jun 15 '18 13:06 danielo515

(i'm curious: did you go for LiveScript because sweetjs didn't answer your expectations?)

vendethiel avatar Jun 15 '18 15:06 vendethiel

Hahaha. Does it look that way? This (the functional one) seems to be a not small but close (meaning distance, not closed) community.

This is a feature I would like to have on both. Why? Because I want to get more functional without going as crazy as purescript (for now) so livescript seems like a good alternative. However, since I can choose using livescript it will be hard to also convince my Co-workers. That's why I want to introduce some features on plain javascript, and for that purpose sweet seems to fit the bill. Ao ideally I want it both places :smile:

Regards

danielo515 avatar Jun 15 '18 17:06 danielo515

For the record, I think this is a decent idea (perhaps the operator should be spelled <||, because it's kind of like <| but for curried functions, which are defined with -->... then again, this feature seems geared towards people who are avoiding using -->, so maybe drawing that parallel isn't useful), but a very low priority one for me to work on, given that it's just sugar for fewer parentheses, that a comparable runtime solution exists, and that, again, it would be for people who are not defining curried functions the LiveScript way (personally, I'm not a fan of the LiveScript curried functions either, but we're probably stuck with them until the next language fork).

rhendric avatar Jun 22 '18 23:06 rhendric

Hello @rhendric , and thanks for partially supporting my suggestion.

Indeed this is geared towards the usage of not automatically curried functions (using -->) but actually curried functions. I find the pipe operator very awkward and introducing another one even weirder is something that does not excites me much 😄

that it's just sugar for fewer parentheses, that a comparable runtime solution exists,

How do you think this works currently ? Because the way I have to use it does not involves parentheses in any way. This is the best I got:

add = (a)->(b)->
    a + b

add 1 <| 2

But that only seems to work for functions with two parameters, if you want to use more than tow things go very lispy

add3 = (a)->(b)->(c)->
    a + b + c

(((add3 1) 2) 3)

Which again, is not that bad unless you have to refactor or verify things.

danielo515 avatar Jun 24 '18 07:06 danielo515

well, if you dont like -->, please explain, how to construct functions. i use --> one liner to generate a set of standard functions and then extract one. .... oke, ive got it. may be avoided....

determin1st avatar Jun 24 '18 18:06 determin1st

I find the pipe operator very awkward and introducing another one even weirder is something that does not excites me much :smile:

I'm rather confused by this comment; you went and named your proposed operator $—after, I assume, Haskell's operator of the same name, which is exactly the same as LiveScript's <|—but you think the latter is very awkward? (Obviously, we can't use $ as an operator in LiveScript; it's a valid identifier, and a rather popular one in web development for over a decade.)

How do you think this works currently ?

I'm not sure I understand the question, because it seems like the answer is just repeating things you already know, but: you can either use more parentheses—add3(1)(2)(3), or, as you wrote, (((add3 1) 2) 3)—or use the function I offered in https://github.com/gkz/LiveScript/issues/1051#issuecomment-397612705, if you accept letting the uncurrying happen at run time instead of compile time.

rhendric avatar Jun 24 '18 22:06 rhendric

I'm rather confused by this comment; you went and named your proposed operator $—after, I assume, Haskell's operator of the same name, which is exactly the same as LiveScript's <|—but you think the latter is very awkward?

I'm also not sure I understand yours, but yes, I find all pipe operators awkward. Not because how they look, they look fine, is the work they require on my keyboard to type. They are on separate keys and they require two separate modifiers, which makes them very hard to type comfortably.

I'm not sure I understand the question,

Sorry about that. I'm very new to LS and I don't know all the options available. Also curried functions does not seem to be very used on LS community.

the answer is just repeating things you already know, but:

No, actually not, I was not aware of the first one you wrote. I use that for JS but I didn't know it was possible on LS too.

if you accept letting the uncurrying happen at run time instead of compile time.

Actually I found a satisfactory solution. And with found I mean that I made it myself gluing some existing things and creating some new ones 😄 Using a combination of LiveScript, LiveScript-next, babel, and two babel plugins I wrote this weekend ( curry-all and all-curried ) I reached a satisfactory solution. Instead of using a special operator for curry, I just make all my functions curried and I call all my functions like they were curried. Since this happens automatically thanks to my plugins, I just write plain livescript and the generated javascript is exactly how I want.

Regards

danielo515 avatar Jun 25 '18 07:06 danielo515

is the work they require on my keyboard to type.

Oh, okay, thanks for explaining.

Also curried functions does not seem to be very used on LS community.

FYI, they're used extensively in the optional LiveScript prelude, prelude-ls, which you can get in the REPL with the -d option (or you can just require it like any other NPM package). If you're already deep into things like Sanctuary, you probably won't find prelude-ls useful, as it's mostly the same stuff without as much Haskelly rigor, but this is the LS-branded approach to FP.

Glad you worked out a satisfactory solution, and welcome to LiveScript!

rhendric avatar Jun 25 '18 14:06 rhendric