muScript: better example formatting
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-03-11 17:10:56 +01:00
parent 516fcea3a1
commit cfbc10387f
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 30 additions and 30 deletions

View File

@ -28,17 +28,13 @@ class ValidExampleTest {
blockBuilder.append('\n').append(split[i]);
}
assertEquals("```", split[i++]);
assertEquals("<details>", split[i++]);
assertEquals("<summary>Result</summary>", split[i++]);
assertEquals("<br>", split[i++]);
assertEquals("", split[i++]);
assertEquals("Result:", split[i++]);
assertEquals("```", split[i]);
StringBuilder resultBuilder = new StringBuilder();
while (!split[++i].equals("```")) {
resultBuilder.append('\n').append(split[i]);
}
i++;
assertEquals("</details>", split[i++]);
final String block = blockBuilder.substring(1);
final String expectedResult = resultBuilder.substring(1);
String result = null;

View File

@ -24,7 +24,9 @@ Order of operation is also supported.
The StdLib comes with additional constants and functions, namely: `PI`, `E`, `round`, `floor`, `ceil`, `abs`, `random`
For example:
<details>
<summary>Example</summary>
<br>
```mu
listOf(
@ -44,10 +46,7 @@ listOf(
round(PI, 2) + floor(E) // round also accepts a second argument: precision, but it can be ommitted to round to a whole number
)
```
<details>
<summary>Result</summary>
<br>
Result:
```
[7, -3, 2, 8, 12, 2, 2, 1024, true, false, true, true, true, 5.1400]
```
@ -60,6 +59,10 @@ XOR/XNOR are just your normal `!=`/`==` operators.
Make sure not to use `||`, as that is the string concatenation operator, not OR.
You can also use ternary conditional operators as you would in java.
<details>
<summary>Example</summary>
<br>
```mu
listOf(
1 < 3 ? "this is correct" : "it is not", // if the condition is true, do the first thing. Else, do the second
@ -70,10 +73,7 @@ listOf(
false | false
)
```
<details>
<summary>Result</summary>
<br>
Result:
```
[this is correct, false, false, true, true, false]
```
@ -89,13 +89,14 @@ Equality operations are supported.
The StdLib comes with some additional functions, namely: `toUpper`, `toLower`, `contains` and `replace`
<details>
<summary>Example</summary>
<br>
```mu
15 || "string one"::toUpper() || 'string Two'::toLower() || "example"::contains("thing") || "yxamply"::replace("y", "e")
```
<details>
<summary>Result</summary>
<br>
Result:
```
15STRING ONEstring twofalseexample
```
@ -111,6 +112,10 @@ In the following example, the objects `object` and `object2` is passed to the sc
The StdLib also contains two objects, namely `date` and `time` which allow reading the current date and time.
They also allow creating date/time objects and comparing them.
<details>
<summary>Example</summary>
<br>
```mu
listOf(
object2.valuename, // This is how you would normally do this
@ -126,10 +131,7 @@ listOf(
date(2020, 5, 10) > date.today
)
```
<details>
<summary>Result</summary>
<br>
Result:
```
[subvalue, subvalue, subvalue, subvalue, One, 20, 64, some parameter, 2023-05-13, 23:55:10, false]
```
@ -142,6 +144,10 @@ You can access their entries with `[]`, just like you would access fields for ob
In function calls, you can use the spread operator (`...`) to use all elements of the list as parameters.
The StdLib also comes with some additional functions, namely `len`, `isEmpty`, `concat`, `filter`, `map`, `flatMap`, `fold` and `forEach`.
<details>
<summary>Example</summary>
<br>
```mu
listOf(
len(listOf(1, 2, 3)),
@ -153,10 +159,7 @@ listOf(
listOf(1, 2, 3, 4)::filter({n->n%2==0})::map({n->n/2}) // you can chain the additional functions for proper functional programming
)
```
<details>
<summary>Result</summary>
<br>
Result:
```
[3, true, [1, 2, 3, 4], 4, 23:55:10, 2, [1, 2]]
```
@ -168,6 +171,10 @@ listOf(
This means you can pass them to methods as arguments, store them in variables or do whatever else you feel like.
A closure consists of a list of arguments (of which the last may be variadic) and some instructions.
<details>
<summary>Example</summary>
<br>
```mu
someFunction = {n -> n * 2} // By assigning a closure to a variable, you get an equivalent to a normal java function
@ -208,10 +215,7 @@ listOf(
someFunction2(1, 2, 3, 4)
)
```
<details>
<summary>Result</summary>
<br>
Result:
```
[2, some expression(s), 10, 7, 7, 2, 1, 1, 12, 24]
```