📚 Przykłady
Hello World
print("Hello, World! 🦊");
Zmienne i operacje
let name = "Kitsune";
let age = 25;
const PI = 3.14159;
print(`Imię: ${name}, wiek: ${age}`);
print(`Pole koła: ${PI * 5 * 5}`);
Funkcje
fn factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
print(factorial(5));
let square = x => x * x;
print(square(7));
Listy i pętle
let fruits = ["apple", "banana", "cherry"];
for (fruit in fruits) {
print("🍎 " + fruit);
}
let squares = [x * x for x in 1..5];
print(squares);
let nums = [1, 2, 3, 4, 5];
let evens = nums.filter(x => x % 2 == 0);
let doubled = nums.map(x => x * 2);
let sum = nums.reduce((a, b) => a + b, 0);
Klasy
class Animal {
init(name) {
this.name = name;
}
speak() {
return this.name + " makes a sound";
}
}
class Dog < Animal {
speak() {
return this.name + " barks!";
}
}
let dog = Dog("Buddy");
print(dog.speak());
Pattern Matching
fn describe(value) {
return match value {
0 -> "zero",
n if n < 0 -> "negative",
n if n < 10 -> "small",
n if n < 100 -> "medium",
_ -> "large"
};
}
print(describe(-5));
print(describe(42));
print(describe(1000));
FizzBuzz
for (i in 1..20) {
let output = when i {
n if n % 15 == 0 -> "FizzBuzz",
n if n % 3 == 0 -> "Fizz",
n if n % 5 == 0 -> "Buzz",
_ -> toString(i)
};
print(output);
}
Prosta gra - zgadywanka
use "math";
let secret = randomInt(1, 100);
let attempts = 0;
fn guess(n) {
attempts = attempts + 1;
if (n == secret) {
print(`Brawo! Zgadłeś w ${attempts} próbach!`);
return true;
}
if (n < secret) {
print("Za mało!");
} else {
print("Za dużo!");
}
return false;
}
guess(50);
guess(75);