Examples

Code Examples

Practical examples showing how to use Flowa for real-world applications.

User Authentication System

let users = {};
 
func register_user(username, password, email) {
    if (username in users) {
        return false;
    }
    users[username] = {
        "email": email,
        "password": auth.hash(password)
    };
    return true;
}
 
func login_user(username, password) {
    if (!(username in users)) {
        return false;
    }
    let stored_hash = users[username]["password"];
    return auth.verify(password, stored_hash);
}
 
func create_session_token(username) {
    let payload = {"username": username};
    return jwt.sign(payload);
}

REST API Handler

func handle_get_users(req, res) {
    let db = sqlite.open("app.db");
    let users = sqlite.query(db, "SELECT id, name, email FROM users");
    sqlite.close(db);
    res.writeHead(200, {"Content-Type": "application/json"});
    res.end(json.stringify(users));
}
 
let server = http.createServer();
server.on("GET", "/api/users", handle_get_users);
server.listen(3000);

Performance Benchmark

Flowa execution speed can be measured using the time module. Here is a simple benchmark loop:

print("Starting intensive benchmark...");
 
let run = 0;
while (run < 5) {
    let sum = 0;
    let i = 0;
    let start = time.now_ms();
    while (i < 100000000) {
        sum = sum + i;
        i = i + 1;
    }
    let elapsed_s = time.since_s(start);
    print("Run: " + tostring(run + 1) + ", Sum: " + tostring(sum) + ", Time: " + tostring(elapsed_s) + "s");
    run = run + 1;
}
 
print("Benchmark complete!");

Class-Based Example

class BankAccount {
    func init(owner, balance) {
        this.owner = owner;
        this.balance = balance;
    }
 
    func deposit(amount) {
        this.balance = this.balance + amount;
        return this.balance;
    }
 
    func withdraw(amount) {
        if (amount > this.balance) {
            throw "Insufficient funds";
        }
        this.balance = this.balance - amount;
        return this.balance;
    }
}
 
let account = new BankAccount("Alice", 1000);
account.deposit(500);
print(account.getBalance());  // 1500

Error Handling Example

func processUser(user) {
    try {
        if (user["age"] < 0) {
            throw "Age cannot be negative";
        }
        // Process user
        return true;
    } catch (e) {
        log.error("Error processing user: " + e["message"]);
        return false;
    } finally {
        print("Cleanup executed");
    }
}

Actor Model Example

actor Worker {
    func init() {
        this.value = 0;
    }
 
    func process(data) {
        print("Processing:", data);
    }
}
 
let worker = new Worker();
send(worker, "process", "Task 1");  // Asynchronous
send(worker, "process", "Task 2");    // Non-blocking

More Examples

Check out the Flowa repository (opens in a new tab) for more examples:

  • examples/01_hello.flowa - Hello World
  • examples/14_server.flowa - HTTP Server
  • examples/15_sqlite.flowa - Database operations
  • tests/ - Test suite with many examples