ilusm.dev

A Tour of ilusm

Learn ilusm by example through short, hands-on exercises

Welcome

Welcome to the ilusm tour! This interactive guide will walk you through the language features with hands-on examples. Each section includes runnable code snippets that you can modify and experiment with.

Prerequisites: Basic programming knowledge. No prior ilusm experience required.

Time: About 30 minutes to complete.

1. Basics

Hello, World!

Let's start with the classic. In ilusm, printing is just prn:

prn("Hello, World!")

Run this in Loveshack (step 1).

Variables

Variables are created by assignment. No let, var, or const needed:

# Simple assignment
x = 42
name = "Alice"
pi = 3.14159

# Immutable with 'def'
def max_size = 100

prn(x)
prn(name)

Values

ilusm has booleans, numbers, strings, lists, and objects:

# Booleans
ok = tru
no = fls

# Numbers
count = 100
price = 19.99
hex = 0xFF

# Strings
msg = "hello"
interp = $"count is {count}"

# Lists
nums = [1, 2, 3, 4, 5]

# Objects
user = {name: "Bob", age: 30}

Exercise: Create Variables

Create variables for your name, age, and favorite number. Print them using string interpolation.

Hint: Use $"name: {nam}" for interpolation.

2. Functions

One-Line Functions

Simple functions can be written in one line:

add(a, b) = a + b
square(x) = x * x
say_hi() = prn("Hi!")

prn(add(5, 3))
prn(square(4))

Block Functions

More complex functions use indented blocks:

factorial(n) =
    if n <= 1: 1 | n * factorial(n - 1)

prn(factorial(5))  # 120

Lambdas

Anonymous functions are perfect for quick operations:

# Lambda syntax: \(params) body
double = \(x) x * 2
add_one = \(x) x + 1

prn(double(5))
prn(add_one(10))

Exercise: Write a Function

Write a function is_even(n) that returns tru if n is even, fls otherwise.

3. Control Flow

If Statements

Simple conditionals:

score = 85

# Expression form - nested if/else with |
grade = if score >= 90: "A" | if score >= 80: "B" | "C"
prn(grade)  # "B"

# Statement form
if score >= 90: prn("A")
if score >= 80: prn("B") | prn("C")

For-Each Loops

Iterate over collections:

fruits = ["apple", "banana", "cherry"]

fruit <- fruits:
    prn(fruit)

# With range
i <- 1..5:
    prn(i)

While Loops

count = 5
whl count > 0:
    prn(count)
    count = count - 1

Pattern Matching

Elegant multi-way branching:

day = 3

# One-line match expression
name = mat day: 1 => "Monday" | 2 => "Tuesday" | 3 => "Wednesday" | _ => "Other"
prn(name)  # "Wednesday"

Exercise: FizzBuzz

Print numbers 1 to 20. For multiples of 3 print "Fizz", multiples of 5 print "Buzz", both print "FizzBuzz".

4. Collections

Lists

Lists are ordered, zero-indexed collections:

nums = [10, 20, 30, 40, 50]

prn(nums[0])    # 10
prn(nums[2])    # 30
prn(len(nums))  # 5

Objects

Objects are key-value mappings:

user = {name: "Alice", age: 30, active: tru}

prn(user.name)
prn(user.age)

# Destructuring
{name, age} = user
prn(name)

Destructuring

# List destructuring
[a, b, c] = [1, 2, 3]

# Object destructuring  
{x, y} = {x: 10, y: 20}

Exercise: Sum a List

Write a function that sums all numbers in a list using a for loop.

5. Pipelines

Basic Pipeline

Pipelines are one of ilusm's most powerful features. They let you chain operations elegantly. The | operator passes the left value as the first argument to the right:

use trl

nums = [1, 2, 3, 4, 5, 6]
even(x) = x / 2 * 2 == x

# Without pipeline
result = trl.map(trl.fil(nums, \(x) even(x)), \(x) x * 2)

# With pipeline - same thing, reads left to right
result = nums | fil \(x) even(x)
              | map \(x) x * 2

prn(result)  # [4, 8, 12]

Real-World Example

use jsn
use trl

# Read, parse, transform, encode
data = '{"users":[{"name":"Alice","score":85},{"name":"Bob","score":92}]}'

result = data | jsn.dec
              | .users
              | fil \(u) u.score > 90
              | map \(u) u.name
              | jsn.enc

prn(result)  # ["Bob"]

Exercise: Pipeline Practice

Given nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], use pipelines to:

  1. Filter even numbers
  2. Square each
  3. Sum the results

6. Modules

Importing

Import from the standard library or local files:

# Standard library
use trl  # list utilities
use txt  # string utilities
use jsn  # JSON

# Local file
use "helpers.ilu"

Standard Library Overview

  • trl - map, filter, fold, sort
  • txt - split, join, trim
  • jsn - JSON encode/decode
  • net - HTTP client/server
  • web - Web framework
  • sql - Database access
  • fs - File system
  • os - OS interface

Exercise: Build a Web Server

Write a web server that responds with "Hello, World!" to GET /

Hint: use the web module - web.app() creates a server, app.get(path, handler) adds routes, app.run(addr) starts it.

Continue Your Journey

Now you know the basics of ilusm. Where to go next?