From 5d1ef0fef26f376465447429a9407cb1b864ca48 Mon Sep 17 00:00:00 2001 From: Michael Floering Date: Fri, 7 Jan 2022 10:12:42 -0800 Subject: [PATCH 1/3] Add to docs - example of parens around functions Follow-up to #247 --- README.md | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b8d9fec..79270e0 100644 --- a/README.md +++ b/README.md @@ -333,6 +333,27 @@ params = { } ``` +
Optionally, you can use parentheses around inner functions + +```python +params = { + width: 50, + height: 30, + onclick: (def(event): + alert("you clicked me") + ), + onmouseover: (def(event): + $(this).css('background', 'red') + ), + onmouseout: (def(event): + # reset the background + $(this).css('background', '') + ) +} +``` + +
+ Note the comma on a new line following a function declaration, it needs to be there to let the compiler know there are more attributes in this object literal, yet it can't go on the same line as the function since it would get parsed as part of the function block. Like Python, however, RapydScript supports new-line shorthand using a `;`, which you could use to place the comma on the same line: ```python @@ -342,6 +363,16 @@ hash = { 'bar': def(): print('bar') } + +// You could do it this way instead if you do not like the ";," +hash = { + 'foo': (def(): + print('foo') + ), + 'bar': (def(): + print('bar') + ), +} ``` It is because of easy integration with JavaScript's native libraries that RapydScript keeps its own libraries to a minimum. For example, it does not implement string interpolation, like native Python. However, by using `sprintf.js` library () you can reproduce the same behavior in RapydScript: @@ -368,9 +399,16 @@ factorial = def(n): if n == 0: return 1 return n * factorial(n-1) + + +factorial = (def(n): + if n == 0: + return 1 + return n * factorial(n-1) +) ``` -This might not seem like much at first, but if you're familiar with JavaScript, you know that this can be extermely useful to the programmer, especially when dealing with nested functions, which are a bit syntactically awkward in Python (it's not immediatelly obvious that those can be copied and assigned to other objects). To illustrate the usefulness, let's create a method that creates and returns an element that changes color while the user keeps the mouse pressed on it. +This might not seem like much at first, but if you're familiar with JavaScript, you know inline functions are extremely useful to the programmer. To illustrate the usefulness, let's create a method that creates and returns an element that changes color while the user keeps the mouse pressed on it. ```python def makeDivThatTurnsGreen(): From 96ceb617ed76f48900f4696752d9012d4e88cbaf Mon Sep 17 00:00:00 2001 From: Michael Floering Date: Fri, 7 Jan 2022 10:17:28 -0800 Subject: [PATCH 2/3] Update README.md --- README.md | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 79270e0..9ffe9aa 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,25 @@ Now you can pass it to jQuery: Another feature of RapydScript is ability to have functions as part of your object literal. JavaScript APIs often take callback/handler functions as part of their input parameters, and RapydScript lets you create such object literal without any quirks/hacks: +```python +params = { + width: 50, + height: 30, + onclick: (def(event): + alert("you clicked me") + ), + onmouseover: (def(event): + $(this).css('background', 'red') + ), + onmouseout: (def(event): + # reset the background + $(this).css('background', '') + ) +} +``` + +The parentheses around the inline functions hel + ```python params = { width: 50, @@ -333,9 +352,10 @@ params = { } ``` -
Optionally, you can use parentheses around inner functions +
+ -```python +# The following is equivalent, if you prefer this style (parentheses around the functions) params = { width: 50, height: 30, @@ -350,11 +370,10 @@ params = { $(this).css('background', '') ) } -```
-Note the comma on a new line following a function declaration, it needs to be there to let the compiler know there are more attributes in this object literal, yet it can't go on the same line as the function since it would get parsed as part of the function block. Like Python, however, RapydScript supports new-line shorthand using a `;`, which you could use to place the comma on the same line: +Note the comma on a new line following a function declaration (like `onmouseover` in the example), it's letting the compiler know there are more attributes in this object literal, yet it can't go on the same line as the function since it would get parsed as part of the function block. Like Python, however, RapydScript supports new-line shorthand using a `;`, which you could use to place the comma on the same line: ```python hash = { @@ -364,7 +383,7 @@ hash = { print('bar') } -// You could do it this way instead if you do not like the ";," +# The following is equivalent, if you do not like ";," hash = { 'foo': (def(): print('foo') From 95962f0ac4b9d6e3ffc53df3a87eb81a9f2a6154 Mon Sep 17 00:00:00 2001 From: Michael Floering Date: Fri, 7 Jan 2022 10:19:38 -0800 Subject: [PATCH 3/3] Update README.md --- README.md | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9ffe9aa..4a24c37 100644 --- a/README.md +++ b/README.md @@ -335,27 +335,11 @@ params = { } ``` -The parentheses around the inline functions hel - -```python -params = { - width: 50, - height: 30, - onclick: def(event): - alert("you clicked me"), - onmouseover: def(event): - $(this).css('background', 'red') - , - onmouseout: def(event): - # reset the background - $(this).css('background', '') -} -``` -
- + Optional: You can surround inline functions with parentheses -# The following is equivalent, if you prefer this style (parentheses around the functions) +``` +# the following is equivalent: params = { width: 50, height: 30, @@ -370,6 +354,7 @@ params = { $(this).css('background', '') ) } +```
@@ -383,7 +368,7 @@ hash = { print('bar') } -# The following is equivalent, if you do not like ";," +# the following is equivalent: hash = { 'foo': (def(): print('foo')