jsy-lang / babel-plugin-offside-js

[deprecated] Babel JSY offside (indention) Javascript dialect. See babel-plugin-jsy-lite
BSD 2-Clause "Simplified" License
8 stars 3 forks source link

Add an operator for calling curried functions #6

Open danielo515 opened 6 years ago

danielo515 commented 6 years ago

Hello, Thanks for your plugin , it looks awesome. I was guessing if you would consider adding an operator for calling curried functions. Currently I'm not sure how a function returned from a function could be executed with this syntax. Ideally I would want use just one symbol or it once:

add $ 1 2 
//translated to
add(1)(2)

Regards

shanewholloway commented 6 years ago

We occasionally run into this challenge in practice. Two solutions:

add
@ 1
@ 2

// or…
add(1) @ 2

The semantics of a new operator would be somewhat similar to what is done for keywords (if, for, while) closing whatever is open upon the next :: token. In this case, the new curry operator would close the previous (. Naming that operator @;:

// JSY source:
add @ 1 @; 2

// Javascript source:
add (1) (2)
danielo515 commented 6 years ago

Well, the first solution looks good enough as a workaround. Can you do

add @ 1
    @ 2

By the way, which version of babel is this intended to be used with ? I'm unable to make it work

shanewholloway commented 6 years ago

The guiding principle behind offside-js/JSY is to use indentation as the primary signal for code blocks. When the "next line" is outdented to the same leader or fewer characters, each open operator on the stack is closed.

// thus:
add @ one
       @ two

// becomes
add ( one
    ( two ))

// e.g.
add( one (two) )

There are special rules for multiple operators. If an operator token is the last on the line, all operators span the content to the outdent. However, all operators after the first that have content trailing on the same will be closed before the next line.

first @ second @ third @
  'some', 'inner', 'args'

// becomes
first( second( third(
   'some', 'inner', 'args' ) ) )

// whereas
first @ second @ third
  'some', 'inner', 'args'

// becomes, with optional implicit commas,
first( second( third )
   , 'some', 'inner', 'args' ) ) )
shanewholloway commented 6 years ago

Both the plugin and the preset works with Babel 6 (Current Stable); the syntax extension works by monkey patching the Babylon parser, which is not a supported extension method of Babel nor Babylon. That said, it has worked for us throughout the lifetime of the offside-js/JSY project, and we are using it in a production product.

By way of working code examples, two smaller project I'd point you at are:

Many of my node and javascript projects use JSY; feel free to look around my repos.

Looking toward the future, I've been working on a non-Babel based pure Javascript source transform. That will expand support to Babel 7, Acorn, and other frameworks. It currently needs more time than I have to give it: sourcemap support, porting unit tests, and implementing some correctness around auto-closing behavior in keyword blocks. I achieved enough with it to have confidence that we'll be able to use JSY syntax in the future without much difficulty. Likely, the priority of the pure-js implementation will rise after Babel 7 is released.

danielo515 commented 6 years ago

Many thanks for your detailed explanation and the examples. All that you have mention looks awesome. I can see that jsy extension is required on the files. Is this correct?

shanewholloway commented 6 years ago

Not required, just a convention. Keeps my editor from going too crazy...

On Jun 14, 2018, at 2:02 AM, Daniel Rodríguez Rivero notifications@github.com wrote:

Many thanks for your detailed explanation and the examples. All that you have mention looks awesome. I can see that jsy extension is required on the files. Is this correct?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.