Update README.md. Add array foreach example. Add better formatting.

Signed-off-by: funkywaddle <paul@funkywaddle.com>
This commit is contained in:
Funky Waddle 2025-11-24 06:07:09 +00:00
parent 2a74e5d5b6
commit f9eaf1a657

View file

@ -22,47 +22,61 @@ Waddle is a modern, expressive programming language designed to be intuitive, re
## Basic Syntax ## Basic Syntax
### Type Declaration ### Type Declaration
#### Basic type definition
```waddle ```waddle
// Basic type definition
Animal { Animal {
// Type implementation // Type implementation
} }
```
// Inheritance #### Inheritance
```waddle
Animal/Dog { Animal/Dog {
// Dog-specific implementations // Dog-specific implementations
} }
```
// Interface definition #### Interface definition
```waddle
@Mammal { @Mammal {
// Interface methods // Interface methods
} }
```
// Interface implementation #### Interface implementation
```waddle
Animal/Dog -> [Mammal, Domesticated] { Animal/Dog -> [Mammal, Domesticated] {
// Multiple interface support // Multiple interface support
} }
``` ```
### Functions ### Functions
#### Function declaration
##### Public function
```waddle ```waddle
// Function declaration
// Public function
+fn calculateArea(width: Integer, height: Integer) -> [Decimal] { +fn calculateArea(width: Integer, height: Integer) -> [Decimal] {
rtn width.multiply(height); rtn width.multiply(height);
} }
```
// Protected Function ##### Protected Function
```waddle
fn calculateArea(width: Integer, height: Integer) -> [Decimal] { fn calculateArea(width: Integer, height: Integer) -> [Decimal] {
rtn width.multiply(height); rtn width.multiply(height);
} }
```
// Private Function ##### Private Function
```waddle
-fn calculateArea(width: Integer, height: Integer) -> [Decimal] { -fn calculateArea(width: Integer, height: Integer) -> [Decimal] {
rtn width.multiply(height); rtn width.multiply(height);
} }
```
// Lambda expression ##### Lambda expression
```waddle
areaCalculator = fn(width: Integer, height: Integer) -> [Decimal] { areaCalculator = fn(width: Integer, height: Integer) -> [Decimal] {
rtn width.multiply(height); rtn width.multiply(height);
} }
@ -90,26 +104,46 @@ result = riskyOperation().attempt {
``` ```
### Iteration ### Iteration
#### Forward iteration
```waddle ```waddle
// Forward iteration
loop(i, 0->10) { loop(i, 0->10) {
my.output.print(i); my.output.print(i);
} }
```
// Foward iteration, with step #### Foward iteration, with step
```waddle
loop(i, 0->10, 2) { loop(i, 0->10, 2) {
my.output.print(i); // outputs 0, 2, 4, 6, 8, 10 my.output.print(i); // outputs 0, 2, 4, 6, 8, 10
} }
```
// Reverse iteration #### Reverse iteration
```waddle
rloop(i, 10->0) { rloop(i, 10->0) {
my.output.print(i); my.output.print(i);
} }
```
// Reverse iteration, with step #### Reverse iteration, with step
```waddle
rloop(i, 10->0, 2) { rloop(i, 10->0, 2) {
my.output.print(i); // outputs 10, 8, 6, 4, 2, 0 my.output.print(i); // outputs 10, 8, 6, 4, 2, 0
} }
```
#### Foreach loop on arrays
```waddle
Integer<> nums.set(<1, 2, 3, 4>);
nums.each(
i -> {
if(i.mod(2).eq(0)){
my.output.print(i); // outputs 2, 4
}
}
);
``` ```